Monday, May 30, 2011

referenced from:


className String|null
The optional name of the Class to declare.
The className will be used as a global name for a created constructor.
If you don't specify it, the class is assumed to be anonymous (new since V1.4).
If you specify it, the name will be stored in the property "declaredClass" in the created prototype.
superclass null|Object|Object[]
This parameter is either
  • null (no base class),
  • an object (a base class) or
  • an array of objects (multiple inheritance).
props Object
An object whose properties are copied (mixed in) to the created prototype after all other inheritance has been solved.
You can add an instance-initialization function by making it a property named "constructor".

Global:

Example: http://stackoverflow.com/questions/2444801/queryreadstore-loads-json-into-datagrid-but-jsonreststore-does-not-from-the-sam


dojo.declare("dojox.data.CustomStore", dojox.data.JsonRestStore, {
    _processResults: function(results, deferred){
        var items = SOME_MAPPING_FUNCTION_HERE(results);
        var count = COUNT_RESULTS_HERE;
        return {totalCount:deferred.fullLength || (deferred.request.count == count ? (deferred.request.start || 0) + count * 2 : count), items: items};
    }
}


To show how the inheritance chain works, we will create a new class derived from my.Thinger:
1
2
3
4
5
6
7
8
9
dojo.declare("my.OtherThinger", [my.Thinger], {
  divisor: 5,
  constructor: function(args){
    console.log('OtherThinger constructor called');
    this.total = this.count / this.divisor;
  }
});
var thing = new my.OtherThinger({ count:50 });
console.log(thing.total); // 10 

Local:


Now we have a 'base class', called my.Thinger.
If we don't want a globally accessible class we can easily make it local (since 1.4):
1
2
3
4
5
6
7
8
9
var localThinger = dojo.declare(null, {
  count: 100,
  constructor: function(args){
    dojo.mixin(this, args);
  }
});
var thing1 = new localThinger();
var thing2 = new localThinger({ count:200 });
console.log(thing1.count, thing2.count);

No comments:

Post a Comment