From 63cbe8dd17e5b80e4eee68e17c5c22f071a263a8 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 17 Jul 2013 14:15:09 +1000 Subject: [PATCH] fix links to site contents, fix it so the site contents page is refreshable --- .../admin/models/site_content_type.js | 6 +++-- .../routes/admin_site_content_edit_route.js | 27 +++++++++++++++++-- .../admin/routes/admin_site_contents_route.js | 5 +++- .../templates/site_contents.js.handlebars | 4 +-- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/admin/models/site_content_type.js b/app/assets/javascripts/admin/models/site_content_type.js index fdda50f56..0e799c0b0 100644 --- a/app/assets/javascripts/admin/models/site_content_type.js +++ b/app/assets/javascripts/admin/models/site_content_type.js @@ -10,12 +10,14 @@ Discourse.SiteContentType = Discourse.Model.extend({}); Discourse.SiteContentType.reopenClass({ findAll: function() { - var contentTypes = Em.A(); + promise = new Em.Deferred(); Discourse.ajax("/admin/site_content_types").then(function(data) { + var contentTypes = Em.A(); data.forEach(function (ct) { contentTypes.pushObject(Discourse.SiteContentType.create(ct)); }); + promise.resolve(contentTypes); }); - return contentTypes; + return promise; } }); diff --git a/app/assets/javascripts/admin/routes/admin_site_content_edit_route.js b/app/assets/javascripts/admin/routes/admin_site_content_edit_route.js index 57362d567..5ada1036e 100644 --- a/app/assets/javascripts/admin/routes/admin_site_content_edit_route.js +++ b/app/assets/javascripts/admin/routes/admin_site_content_edit_route.js @@ -13,7 +13,29 @@ Discourse.AdminSiteContentEditRoute = Discourse.Route.extend({ }, model: function(params) { - return {content_type: params.content_type}; + var list = this.controllerFor('adminSiteContents').get('model'); + var model; + + // ember routing is fun ... this is what happens + // + // linkTo creates an Ember.LinkView , it marks an with the class "active" + // if the "context" of this dynamic route is equal to the model in the linkTo + // the route "context" is set here, so we want to make sure we have the exact + // same object, from Ember we have: + // + // if (handlerInfo.context !== object) { return false; } + // + // we could avoid this hack if Ember just compared .serialize(model) with .serialize(context) + // + // alternatively we could use some sort of identity map + + list.forEach(function(orig){ + if(orig.get("content_type") === params.content_type){ + model = orig; + } + }); + + return model; }, renderTemplate: function() { @@ -26,6 +48,7 @@ Discourse.AdminSiteContentEditRoute = Discourse.Route.extend({ }, setupController: function(controller, model) { + controller.set('loaded', false); controller.setProperties({ model: model, @@ -33,7 +56,7 @@ Discourse.AdminSiteContentEditRoute = Discourse.Route.extend({ saved: false }); - Discourse.SiteContent.find(Em.get(model, 'content_type')).then(function (sc) { + Discourse.SiteContent.find(model.get('content_type')).then(function (sc) { controller.set('content', sc); controller.set('loaded', true); }); diff --git a/app/assets/javascripts/admin/routes/admin_site_contents_route.js b/app/assets/javascripts/admin/routes/admin_site_contents_route.js index 69a2e29f4..b5399eb83 100644 --- a/app/assets/javascripts/admin/routes/admin_site_contents_route.js +++ b/app/assets/javascripts/admin/routes/admin_site_contents_route.js @@ -13,9 +13,12 @@ Discourse.AdminSiteContentsRoute = Discourse.Route.extend({ }, renderTemplate: function(controller, model) { - controller.set('model', model); this.render('admin/templates/site_contents', {into: 'admin/templates/admin'}); this.render('admin/templates/site_contents_empty', {into: 'admin/templates/site_contents'}); + }, + + setupController: function(controller, model) { + controller.set('model', model); } }); diff --git a/app/assets/javascripts/admin/templates/site_contents.js.handlebars b/app/assets/javascripts/admin/templates/site_contents.js.handlebars index 173999ab4..970b88943 100644 --- a/app/assets/javascripts/admin/templates/site_contents.js.handlebars +++ b/app/assets/javascripts/admin/templates/site_contents.js.handlebars @@ -2,7 +2,7 @@

{{i18n admin.site_content.edit}}

    - {{#each type in content}} + {{#each type in model}}
  • {{#linkTo 'adminSiteContentEdit' type}}{{type.title}}{{/linkTo}}
  • @@ -13,4 +13,4 @@
    {{outlet}}
    -
\ No newline at end of file +