Created
September 13, 2013 17:28
-
-
Save bryanforbes/6553603 to your computer and use it in GitHub Desktop.
Store-backed widget example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// my/Application.js | |
define([ | |
'dojo/store/JsonRest', | |
'./Widget', | |
'dojo/domReady!' | |
], function (JsonRest, Widget) { | |
return { | |
initialize: function () { | |
var store = new JsonRest({ target: '/target/' }), | |
ui = new Widget({ | |
store: store | |
}).placeAt(document.body); | |
ui.startup(); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- my/tests/Widget.html --> | |
<script> | |
require([ | |
'dojo/request', | |
'dojo/store/Memory', | |
'my/Widget', | |
'dojo/domReady!' | |
], function (request, Memory, Widget) { | |
request.get('./fixtures/test.json', { | |
handleAs: 'json' | |
}).then(function (data) { | |
var store = new Memory({ data: data }), | |
widget = new Widget({ store: store }, "widget"); | |
widget.startup(); | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// my/Widget.js | |
define([ | |
'dojo/_base/declare', | |
'dijit/_WidgetBase', | |
'dojo/when' | |
], function (declare, _WidgetBase, when) { | |
return declare(_WidgetBase, { | |
store: null, | |
_setStoreAttr: function (store) { | |
// assign the store to other widgets | |
this._set('store', store); | |
if (this.store && this._started) { | |
this.render(); | |
} | |
}, | |
startup: function () { | |
this.startup = function () {}; | |
this.inherited(arguments); | |
if (this.store) { | |
this.render(); | |
} | |
}, | |
render: function () { | |
if (!this.store) { | |
return; | |
} | |
var self = this; | |
when(this.store.query({})).then(function (results) { | |
results.forEach(function (item) { | |
self.renderItem(item); | |
}); | |
}); | |
}, | |
renderItem: function (item) { | |
// render an item | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment