为话语添加新功能

Adding new feature to discourse

本文关键字:新功能 添加 话语      更新时间:2023-09-26

我正在尝试在现有的代码话语余烬轨道应用程序中添加监视列表功能

我添加了以下代码

 Discourse.Route.buildRoutes(function() {
    var router = this;
    this.resource('watchLists', { path: '/watch_lists' }, function() {
        this.resource('watchList', {path: ':watch_list_id'});
     });
  });

在余烬控制器中

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

在余烬模型中

   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);
      });
   });

在余烬视图中 js

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

在余烬路由 js 中

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

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

任何机构都能指出我做错的地方吗?

我看到两个可能的问题。

首先,您可能希望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);
      }
    });
  }
});