From 1ca43d3bb958d76ca7c121ba4b64c68f52a90ea5 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 26 Feb 2015 15:54:39 -0500 Subject: [PATCH] Store and Adapter support for finding by id, updating a simple record --- app/assets/javascripts/discourse.js | 3 + .../discourse/adapters/rest.js.es6 | 60 +++++++++++++------ .../initializers/inject-objects.js.es6 | 1 + .../javascripts/discourse/models/store.js.es6 | 6 ++ 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index c426b54e658..f72cb294275 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -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 = {}; diff --git a/app/assets/javascripts/discourse/adapters/rest.js.es6 b/app/assets/javascripts/discourse/adapters/rest.js.es6 index a01491da971..a00cdd1841c 100644 --- a/app/assets/javascripts/discourse/adapters/rest.js.es6 +++ b/app/assets/javascripts/discourse/adapters/rest.js.es6 @@ -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; diff --git a/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 b/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 index ba0be1f986c..7950eb467bb 100644 --- a/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 +++ b/app/assets/javascripts/discourse/initializers/inject-objects.js.es6 @@ -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); diff --git a/app/assets/javascripts/discourse/models/store.js.es6 b/app/assets/javascripts/discourse/models/store.js.es6 index fed39da1df6..8fcd0d86ae5 100644 --- a/app/assets/javascripts/discourse/models/store.js.es6 +++ b/app/assets/javascripts/discourse/models/store.js.es6 @@ -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); } + });