Reputation: 12904
I write ctor's of My dojo Widgets like this.
constructor: function(params){
var r = this.inherited(arguments);
this.params = params;
//other ctor works
return r;
}
I instanciate the widget with JSON response as ctor argument. like new MyWidget(res)
and the JSON response contains an id
attribute like {id: 5, text: 'Hallo'}
Now _WidgetBase
constructor treats this id as Widget Id and throws Tried to register widget with id==6 but that id is already registered
. cause there may be some other widget that is also getting id: 6
for another subject.
and I cannot change the JSON response to output like subject_id
as it will need changing a lot of things.
and I need to get that id after widget instantiation.
So What would be a good design to handle this problem ?
Upvotes: 0
Views: 621
Reputation: 1978
Do you actually need to map the json properties to your widget directly ? If not, then just have a json property in your widget, and when instantiating : new myWidget({jsonObj:res}); then you could have a getJsonId() function that would return this.jsonObj.id;
Upvotes: 0
Reputation: 7352
Interesting question! Here is my solution: http://jsfiddle.net/phusick/z9u8a/
var Widget = declare(_WidgetBase, {
postscript: function() {
var id = arguments[0]["id"];
delete arguments[0]["id"];
this.inherited(arguments);
this.params.id = id;
},
constructor: function(params) {
// your constructor here
}
});
var widget1 = new Widget({ id: 1, text: 'Hallo 1'});
var widget2 = new Widget({ id: 1, text: 'Hallo 2'});
EDIT: I did some clean up, so all you need is to add postscript
method.
Upvotes: 1