【问题标题】:Adding new feature to discourse为话语添加新功能
【发布时间】:2013-09-26 11:54:50
【问题描述】:

我正在尝试在现有的代码话语 ember rails 应用程序中添加 WatchList 功能

我添加了以下代码

 Discourse.Route.buildRoutes(function() {
    var router = this;

    this.resource('watchLists', { path: '/watch_lists' }, function() {
        this.resource('watchList', {path: ':watch_list_id'});
     });
  });

在 ember 控制器中

 Discourse.WatchListsController = Discourse.ObjectController.extend({});

在 ember 模型中

   Discourse.WatchList = Discourse.Model.extend({});

   Discourse.WatchList.reopenClass({
      find: function() {
          jQuery.getJSON("watch_lists").then(function(json) {
          var watch_lists = json.watch_lists.map(function(attrs) {
          return Discourse.WatchList.create(attrs);
      });
   });

在 ember 视图中的 js

   Discourse.WatchListsView = Ember.View.extend({});

在 ember 路由 js 中

     Discourse.WatchListsRoute = Discourse.Route.extend({
         model: function() {
            return Discourse.WatchList.find();   
         }
    });

当我渲染把手模板时,我得到了一个 WatchListsController 对象,其中没有我们从 ajax 获得的数据。

谁能指出我哪里做错了。

【问题讨论】:

    标签: javascript ruby-on-rails ember.js discourse


    【解决方案1】:

    我看到两个可能的问题。

    首先,您可能希望 WatchListsController 扩展 Discourse.ArrayController,而不是 Discourse.ObjectController

    其次,您发布的示例代码中的 reopen 块不是有效的 JavaScript。我数了四个{,但只有两个}。你可能想要这样的东西:

    Discourse.WatchList.reopenClass({
      find: function() {
        return jQuery.getJSON("watch_lists").then(function(json) {
          return json.watch_lists.map(function(attrs) {
            return Discourse.WatchList.create(attrs);
          }
        });
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多