Store and Adapter support for finding by id, updating a simple record

This commit is contained in:
Robin Ward 2015-02-26 15:54:39 -05:00
parent 3d3b70f4bb
commit 1ca43d3bb9
4 changed files with 53 additions and 17 deletions

View File

@ -126,3 +126,6 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
}.property() }.property()
}); });
// TODO: Remove this, it is in for backwards compatibiltiy with plugins
Discourse.HasCurrentUser = {};

View File

@ -1,29 +1,52 @@
const ADMIN_MODELS = ['plugin']; const ADMIN_MODELS = ['plugin'];
function plural(type) {
return type + 's';
}
function pathFor(type) {
const path = "/" + plural(type);
if (ADMIN_MODELS.indexOf(type) !== -1) {
return "/admin/" + path;
}
return path;
}
const _identityMap = {}; const _identityMap = {};
const RestModel = Ember.Object.extend({
update(attrs) {
const self = this;
return this.store.update(this.get('__type'), this.get('id'), attrs).then(function(result) {
self.setProperties(attrs);
return result;
});
}
});
export default Ember.Object.extend({ export default Ember.Object.extend({
serverName(type) {
return Ember.String.underscore(type + 's');
},
pathFor(type, id) {
let path = "/" + this.serverName(type);
if (ADMIN_MODELS.indexOf(type) !== -1) { path = "/admin/" + path; }
if (id) { path += "/" + id; }
return path;
},
findAll(type) { findAll(type) {
var self = this; var self = this;
return Discourse.ajax(pathFor(type)).then(function(result) { return Discourse.ajax(this.pathFor(type)).then(function(result) {
return result[plural(type)].map(obj => self._hydrate(type, obj)); return result[self.serverName(type)].map(obj => self._hydrate(type, obj));
}); });
}, },
find(type, id) {
var self = this;
return Discourse.ajax(this.pathFor(type, id)).then(function(result) {
return self._hydrate(type, result[self.serverName(type)]);
});
},
update(type, id, attrs) {
const data = {};
data[this.serverName(type)] = attrs;
return Discourse.ajax(this.pathFor(type, id), { method: 'PUT', data });
},
_hydrate(type, obj) { _hydrate(type, obj) {
if (!obj) { throw "Can't hydrate " + type + " of `null`"; } if (!obj) { throw "Can't hydrate " + type + " of `null`"; }
if (!obj.id) { throw "Can't hydrate " + type + " without an `id`"; } if (!obj.id) { throw "Can't hydrate " + type + " without an `id`"; }
@ -37,7 +60,10 @@ export default Ember.Object.extend({
return existing; return existing;
} }
const klass = this.container.lookupFactory('model:' + type) || Ember.Object; obj.store = this;
obj.__type = type;
const klass = this.container.lookupFactory('model:' + type) || RestModel;
const model = klass.create(obj); const model = klass.create(obj);
_identityMap[type][obj.id] = model; _identityMap[type][obj.id] = model;
return model; return model;

View File

@ -44,6 +44,7 @@ export default {
app.register('current-user:main', Discourse.User.current(), { instantiate: false }); app.register('current-user:main', Discourse.User.current(), { instantiate: false });
app.inject('component', 'currentUser', 'current-user:main'); app.inject('component', 'currentUser', 'current-user:main');
app.inject('route', 'currentUser', 'current-user:main');
app.inject('controller', 'currentUser', 'current-user:main'); app.inject('controller', 'currentUser', 'current-user:main');
app.register('store:main', Store); app.register('store:main', Store);

View File

@ -2,5 +2,11 @@ export default Ember.Object.extend({
findAll(type) { findAll(type) {
const adapter = this.container.lookup('adapter:' + type) || this.container.lookup('adapter:rest'); const adapter = this.container.lookup('adapter:' + type) || this.container.lookup('adapter:rest');
return adapter.findAll(type); return adapter.findAll(type);
},
find(type, id) {
const adapter = this.container.lookup('adapter:' + type) || this.container.lookup('adapter:rest');
return adapter.find(type, id);
} }
}); });