Skip to content

Instantly share code, notes, and snippets.

@bryanforbes
Created September 13, 2013 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanforbes/6553603 to your computer and use it in GitHub Desktop.
Save bryanforbes/6553603 to your computer and use it in GitHub Desktop.
Store-backed widget example
// 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();
}
};
});
<!-- 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>
// 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