PDA

View Full Version : [CLOSED] Publish via Bus through DirectEvent



cwolcott
Jan 05, 2015, 11:14 PM
I have passed simple variables (String, Integer) through the MessageBus and ExtraParameters, but never an Array within the data structure.

On the client side I gather several data items, one of which is a list of selected Ids from a combobox, and publish them via the MessageBus.

Building the list of product ids from the ComboBox that is multiSelect works, but is there a better way? Produces [31, 64].



...
var products = new Array();
for (var I = 0; I < App.comboBoxProducts.getSubmitArray().length; i++)
products.push(App.comboBoxProducts.getSubmitArray( )[i].value);

var data = new Array();
data[0] = Ext.String.trim(App.comments.getValue());
data[1] = products;
Ext.net.Bus.publish('RequestDialog.Finalize', data);
...


I have a MessageBusDirectEvent that will catch the message and call the direct event passing the data.

Am I passing the Array correctly or does it need to be encoded?


...
<ext:MessageBusDirectEvent Name="" onEvent="Finalize">
<ExtraParams>
<ext:Parameter Name="Comments" Value="data[0]" Mode="Raw" />
<ext:Parameter Name="Products" Value="data[1]" Mode="Raw" />
</ExtraParams>
</ext:MessageBusDirectEvent>
...


Finally in the direct event I need to iterate of the list of product Ids that were selected.

When I grab the extraParameter what else do I need to do to get it back into an Array?



var products = e.ExtraParameters["Products"];


I will try to put together a simple example if it will help.

Daniil
Jan 06, 2015, 1:06 AM
Hi Chris,


Building the list of product ids from the ComboBox that is multiSelect works, but is there a better way?

Using the getSubmitArray method is OK, but it looks you can simplify the code just using a ComboBox's getValue method. It appears to return an array that you need.


Am I passing the Array correctly or does it need to be encoded?

It is OK without encoding.


When I grab the extraParameter what else do I need to do to get it back into an Array?

Please use JSON.Deserialize().

int[] products = JSON.Deserialize<int[]>(e.ExtraParams["Products"]);

Here I have put all the pieces together.

Example

<%@ Page Language="C#" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
Store store = this.comboBoxProducts.GetStore();
store.DataSource = new object[]
{
new object[] { 1, "Item 1" },
new object[] { 2, "Item 2" },
new object[] { 3, "Item 3" }
};
}
}

protected void Finalize(object sender, DirectEventArgs e)
{
int[] products = JSON.Deserialize<int[]>(e.ExtraParams["Products"]);

X.Msg.Alert("Finalize", products.Length).Show();
}
</script>

<!DOCTYPE html>

<html>
<head runat="server">
<title>Ext.NET v2 Example</title>

<script>
var publish = function () {
var data = [];

data[0] = "Comments";
data[1] = App.comboBoxProducts.getValue();
Ext.net.Bus.publish('RequestDialog.Finalize', data);
};
</script>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:ComboBox
ID="comboBoxProducts"
runat="server"
DisplayField="text"
ValueField="value"
MultiSelect="true">
<Store>
<ext:Store runat="server">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="value" Type="Int" />
<ext:ModelField Name="text" />
</Fields>
</ext:Model>
</Model>
<Reader>
<ext:ArrayReader />
</Reader>
</ext:Store>
</Store>
</ext:ComboBox>

<ext:Button runat="server" Text="Publish">
<Listeners>
<Click Fn="publish" />
</Listeners>
<MessageBusDirectEvents>
<ext:MessageBusDirectEvent Name="" OnEvent="Finalize">
<ExtraParams>
<ext:Parameter Name="Comments" Value="data[0]" Mode="Raw" />
<ext:Parameter Name="Products" Value="data[1]" Mode="Raw" />
</ExtraParams>
</ext:MessageBusDirectEvent>
</MessageBusDirectEvents>
</ext:Button>
</form>
</body>
</html>

cwolcott
Jan 06, 2015, 11:55 AM
Perfect. Thank you for the example. Please close the thread.