mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 17:33:37 +08:00
Store and Adapter support for finding by id, updating a simple record
This commit is contained in:
parent
3d3b70f4bb
commit
1ca43d3bb9
|
@ -126,3 +126,6 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
|
|||
}.property()
|
||||
|
||||
});
|
||||
|
||||
// TODO: Remove this, it is in for backwards compatibiltiy with plugins
|
||||
Discourse.HasCurrentUser = {};
|
||||
|
|
|
@ -1,29 +1,52 @@
|
|||
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 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({
|
||||
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) {
|
||||
var self = this;
|
||||
return Discourse.ajax(pathFor(type)).then(function(result) {
|
||||
return result[plural(type)].map(obj => self._hydrate(type, obj));
|
||||
return Discourse.ajax(this.pathFor(type)).then(function(result) {
|
||||
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) {
|
||||
if (!obj) { throw "Can't hydrate " + type + " of `null`"; }
|
||||
if (!obj.id) { throw "Can't hydrate " + type + " without an `id`"; }
|
||||
|
@ -37,7 +60,10 @@ export default Ember.Object.extend({
|
|||
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);
|
||||
_identityMap[type][obj.id] = model;
|
||||
return model;
|
||||
|
|
|
@ -44,6 +44,7 @@ export default {
|
|||
|
||||
app.register('current-user:main', Discourse.User.current(), { instantiate: false });
|
||||
app.inject('component', 'currentUser', 'current-user:main');
|
||||
app.inject('route', 'currentUser', 'current-user:main');
|
||||
app.inject('controller', 'currentUser', 'current-user:main');
|
||||
|
||||
app.register('store:main', Store);
|
||||
|
|
|
@ -2,5 +2,11 @@ export default Ember.Object.extend({
|
|||
findAll(type) {
|
||||
const adapter = this.container.lookup('adapter:' + type) || this.container.lookup('adapter:rest');
|
||||
return adapter.findAll(type);
|
||||
},
|
||||
|
||||
find(type, id) {
|
||||
const adapter = this.container.lookup('adapter:' + type) || this.container.lookup('adapter:rest');
|
||||
return adapter.find(type, id);
|
||||
}
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user