Apr 22, 2010, 6:00 AM
[CLOSED] Add or replace item in Store
Hello,
in my application I am adding items to a store via DirectMethod. This works fine.
The items are added if the IDProperty is not already contained in the store.
If it´s already contained, then the item is replaced.
This is exactly what I need.
Now I want to optimize my code and add in some situations items only via JavaScript (without DirectMethod).
But this does not really work. Items of the same Id are added several times and not replaced.
The Js-Code looks like this:
But with this sample page I also have a problem, because am getting the error "g is undefined" when I use the DirectMethod,
and I do not have any idea why. In my "real" application the DirectMethod works fine.
When I use "AddSortedRecord" then I do not get the error, but the DirectMethod adds the Id also multiple times.
So... what to do?
What is the most efficient way to replace (not add) data to a store in JavaScript and in C# via DirectMethod?
Regards,
Martin
in my application I am adding items to a store via DirectMethod. This works fine.
The items are added if the IDProperty is not already contained in the store.
If it´s already contained, then the item is replaced.
This is exactly what I need.
Now I want to optimize my code and add in some situations items only via JavaScript (without DirectMethod).
But this does not really work. Items of the same Id are added several times and not replaced.
The Js-Code looks like this:
<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
public class Item
{
public string Id { get; set; }
public string Text { get; set; }
}
[DirectMethod]
public void AddItemDirect()
{
var value = TextFieldItem.Text;
var id = NumberFieldId.Value;
if (!String.IsNullOrEmpty(value))
{
Item t = new Item();
t.Id = value;
t.Text = value;
Store1.AddRecord(t);
}
}
</script>
<ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="Script" >
<script type="text/javascript" language="javascript">
var AddItemJavaScript = function(value, id, store)
{
if (value != undefined && value != '')
{
store.add(new store.recordType({ Id: id, Text: value }));
}
}
</script>
</ext:ResourcePlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:Store ID="Store1" runat="server" AutoLoad="true" ShowWarningOnFailure="true">
<Proxy>
<ext:PageProxy />
</Proxy>
<Reader>
<ext:JsonReader IDProperty="Id" TotalProperty="total">
<Fields>
<ext:RecordField Name="Id" Mapping="Id" />
</Fields>
</ext:JsonReader>
</Reader>
<DirectEventConfig Timeout="120000" />
<Listeners>
<LoadException Handler="Ext.MessageBox.alert('Load failed', response.statusText);" />
</Listeners>
</ext:Store>
<ext:FormPanel ID="BasicPanel" runat="server" Layout="Row" Width="500">
<Items>
<ext:GridPanel ID="GridPanel1" runat="server" StripeRows="true" Title="Add Test"
StoreID="Store1" Width="600" Height="350">
<ColumnModel runat="server">
<Columns>
<ext:Column ColumnID="Id" Header="Id" DataIndex="Id" />
</Columns>
</ColumnModel>
<View>
<ext:GridView Id="GridView" runat="server" />
</View>
</ext:GridPanel>
<ext:NumberField ID="NumberFieldId" runat="server" FieldLabel="Enter an id" Text="1" />
<ext:TextField ID="TextFieldItem" runat="server" FieldLabel="Enter value" />
</Items>
<Buttons>
<ext:Button ID="ButtonJavaScript" Text="Add via JavaScript" runat="server">
<Listeners>
<Click Handler="AddItemJavaScript(#{TextFieldItem}.getValue(),#{NumberFieldId}.getValue(), #{Store1});" />
</Listeners>
</ext:Button>
<ext:Button ID="ButtonDirectMethod" Text="Add via DirectMethod" runat="server">
<Listeners>
<Click Handler="#{DirectMethods}.AddItemDirect();" />
</Listeners>
</ext:Button>
</Buttons>
</ext:FormPanel>
</form>
</body>
</html>
But with this sample page I also have a problem, because am getting the error "g is undefined" when I use the DirectMethod,
and I do not have any idea why. In my "real" application the DirectMethod works fine.
When I use "AddSortedRecord" then I do not get the error, but the DirectMethod adds the Id also multiple times.
So... what to do?
What is the most efficient way to replace (not add) data to a store in JavaScript and in C# via DirectMethod?
Regards,
Martin