Sep 01, 2014, 10:29 AM
[CLOSED] Extending JsonWriter with a custom writer, but not coming through?
Hi,
Perhaps I am missing how to do this but I am trying to create a custom writer for my tree panel story proxy so that when I delete nodes, I don't send all the data to the server, just the node id to delete.
To do that, I am trying a variation of the answer to this question:
http://stackoverflow.com/questions/8...g-extra-fields
But my variation is to subclass JsonWriter, to encapsulate this. When I do this, however, looking at the script generated by Ext.NET, I do not see my custom writer.
Here is an example:
c#
The client side for this class is this:
1. Run the above (you may need to adjust the register tag once the writer class is added in)
2. Select the leaf node
3. Press delete
Notice the Loader URL and Destroy URL are intentionally not created - I don't need it - I just want to prove that only id is sent to the destroy URL, even though the URL itself will return a 404!
But in the console, you will not see my console.log statement (as the script does not instantiate my client side writer).
When I look in the script output, the writer is simply this:
Have I missed something on how to get this to work (probably something obvious!) or is this a limitation at the moment?
Thanks!
Perhaps I am missing how to do this but I am trying to create a custom writer for my tree panel story proxy so that when I delete nodes, I don't send all the data to the server, just the node id to delete.
To do that, I am trying a variation of the answer to this question:
http://stackoverflow.com/questions/8...g-extra-fields
But my variation is to subclass JsonWriter, to encapsulate this. When I do this, however, looking at the script generated by Ext.NET, I do not see my custom writer.
Here is an example:
c#
public class CustomWriter : JsonWriter
{
public override string InstanceOf
{
get { return "Ext.ux.CategoryNodeWriter"; }
}
}
(I notice there is only an InstanceOf that I can override, and although there is a Type, I am not sure I can (or want) to override it - I would like to keep it as Json).The client side for this class is this:
Ext.define('Ext.ux.CategoryNodeWriter', {
extend: 'Ext.data.writer.Json',
alias: 'writer.categorynodewriter',
getRecordData: function(record, operation) {
console.log("getRecordData");
return operation.action === 'destroy' ? { 'id': record.get('id') } : this.callParent(arguments);
}
});
A sample ASPX to put this together (which includes the above JavaScript):<%@ Page Language="C#" %>
<%@ Register TagPrefix="cc1" Namespace="Ext.Net2.Tests.Trees.CustomWriter" Assembly="Ext.Net2.Tests" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Title</title>
<ext:ResourcePlaceHolder Mode="ScriptFiles" />
<script>
// #####################################################################
// Custom Writer for category to control what data is sent to the server
// #####################################################################
Ext.define('Ext.ux.CategoryNodeWriter', {
extend: 'Ext.data.writer.Json',
alias: 'writer.categorynodewriter',
getRecordData: function(record, operation) {
console.log("getRecordData");
return operation.action === 'destroy' ? { 'id': record.get('id') } : this.callParent(arguments);
}
});
</script>
</head>
<body>
<ext:ResourceManager runat="server" />
<ext:TreePanel ID="TreePanel1" runat="server" Width="300" Height="200">
<TopBar>
<ext:Toolbar runat="server">
<Items>
<ext:Button Text="Delete" Icon="Delete">
<Listeners>
<Click Handler="
var tree = #{TreePanel1},
node = tree.getSelectionModel().getSelection()[0];
if (!node) return;
node.remove();
tree.getStore().sync();
" />
</Listeners>
</ext:Button>
</Items>
</ext:Toolbar>
</TopBar>
<Root>
<ext:Node Text="Tasks" Expanded="true">
<Children>
<ext:Node Icon="Folder" Text="Delete this" Leaf="true">
<CustomAttributes>
<ext:ConfigItem Name="id" Value="categoryNode1" Mode="Value" />
<ext:ConfigItem Name="task" Value="Project: Shopping" Mode="Value" />
<ext:ConfigItem Name="user" Value="Tommy Maintz" Mode="Value" />
</CustomAttributes>
</ext:Node>
</Children>
</ext:Node>
</Root>
<Store>
<ext:TreeStore runat="server" AutoSync="false">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="task" />
<ext:ModelField Name="user" />
</Fields>
</ext:Model>
</Model>
<Proxy>
<ext:AjaxProxy Url="TreeLoader.ashx">
<API Destroy="Destroy.ashx" />
<Writer>
<cc1:CustomWriter />
</Writer>
</ext:AjaxProxy>
</Proxy>
</ext:TreeStore>
</Store>
</ext:TreePanel>
</body>
</html>
Steps:1. Run the above (you may need to adjust the register tag once the writer class is added in)
2. Select the leaf node
3. Press delete
Notice the Loader URL and Destroy URL are intentionally not created - I don't need it - I just want to prove that only id is sent to the destroy URL, even though the URL itself will return a 404!
But in the console, you will not see my console.log statement (as the script does not instantiate my client side writer).
When I look in the script output, the writer is simply this:
writer:{type:"json"}
One workaround I found was I could implement getRecordData in my C# definition of my custom writer, for example: public override JFunction GetRecordData
{
get { return new JFunction("myGetRecordData"); }
}
However, it means I don't get a fully encapsulated writer (I could of course configure this class to have a property so that the user could pass in the string or jFunction for the GetRecordData implementation and I could then encapsulate it inside my own components perhaps, but ideally I'd like to be able to use my own client side writer.Have I missed something on how to get this to work (probably something obvious!) or is this a limitation at the moment?
Thanks!
Last edited by Daniil; Sep 04, 2014 at 3:05 PM.
Reason: [CLOSED]