Javascript 余烬路线混合,模型

Javascript 余烬路线混合,模型,javascript,ember.js,Javascript,Ember.js,我正在使用ember cli生成我的项目结构。当我使用Mixin和模型扩展Route类时。模型不再显示在我的插座中 controllers/application.js::(ActiveclasscontrollerMixin仅用于提供应用程序级属性,用于确定哪个类属性处于活动状态-我提供了另一个mixin ActiveclassrouteMixin.js作为参考,它与路由的模型挂钩一起使用时会导致问题。) 控制器/posts.js import Ember from 'ember'; var

我正在使用ember cli生成我的项目结构。当我使用Mixin和模型扩展Route类时。模型不再显示在我的插座中

controllers/application.js::(ActiveclasscontrollerMixin仅用于提供应用程序级属性,用于确定哪个类属性处于活动状态-我提供了另一个mixin ActiveclassrouteMixin.js作为参考,它与路由的模型挂钩一起使用时会导致问题。)

控制器/posts.js

import Ember from 'ember';
var PostsController = Ember.ArrayController.extend();
export default PostsController;
import Ember from 'ember';


export default Ember.Mixin.create({    

    setupController: function(controller) {
        this._super(controller);
        var routeName = this.get('routeName');
        this.controllerFor('application').setActiveClass(routeName);
      }

});
import Ember from 'ember';
import ActiveclassrouteMixin from 'embercliapp/mixins/activeclassroute';

var posts = [{
    id: '1',
    title: "Rails is Omakase",
    author: {
        name: "d2h"
    },
    date: new Date('12-27-2012'),
    excerpt: "There are lots of à la carte software environments in this world. Places where in order to eat, you must first carefully look over the menu of options to order exactly what you want.",
    body: "I want this for my ORM, I want that for my template language, and let's finish it off with this routing library. Of course, you're going to have to know what you want, and you'll rarely have your horizon expanded if you always order the same thing, but there it is. It's a very popular way of consuming software.\n\nRails is not that. Rails is omakase."
}, {
    id: '2',
    title: "The Parley Letter",
    author: {
        name: "d2h"
    },
    date: new Date('12-24-2012'),
    excerpt: "My [appearance on the Ruby Rogues podcast](http://rubyrogues.com/056-rr-david-heinemeier-hansson/) recently came up for discussion again on the private Parley mailing list.",
    body: "A long list of topics were raised and I took a time to ramble at large about all of them at once. Apologies for not taking the time to be more succinct, but at least each topic has a header so you can skip stuff you don't care about.\n\n### Maintainability\n\nIt's simply not true to say that I don't care about maintainability. I still work on the oldest Rails app in the world."
}];

export default Ember.Route.extend({
    model: function() {
        return posts;
    }
});
模板/posts.hbs(当routes/posts.js时,此模板工作正常,仅在扩展Ember.Route对象时覆盖模型,并显示所有2篇文章)

mixins/ActiveclassrouteMixin.js

import Ember from 'ember';
var PostsController = Ember.ArrayController.extend();
export default PostsController;
import Ember from 'ember';


export default Ember.Mixin.create({    

    setupController: function(controller) {
        this._super(controller);
        var routeName = this.get('routeName');
        this.controllerFor('application').setActiveClass(routeName);
      }

});
import Ember from 'ember';
import ActiveclassrouteMixin from 'embercliapp/mixins/activeclassroute';

var posts = [{
    id: '1',
    title: "Rails is Omakase",
    author: {
        name: "d2h"
    },
    date: new Date('12-27-2012'),
    excerpt: "There are lots of à la carte software environments in this world. Places where in order to eat, you must first carefully look over the menu of options to order exactly what you want.",
    body: "I want this for my ORM, I want that for my template language, and let's finish it off with this routing library. Of course, you're going to have to know what you want, and you'll rarely have your horizon expanded if you always order the same thing, but there it is. It's a very popular way of consuming software.\n\nRails is not that. Rails is omakase."
}, {
    id: '2',
    title: "The Parley Letter",
    author: {
        name: "d2h"
    },
    date: new Date('12-24-2012'),
    excerpt: "My [appearance on the Ruby Rogues podcast](http://rubyrogues.com/056-rr-david-heinemeier-hansson/) recently came up for discussion again on the private Parley mailing list.",
    body: "A long list of topics were raised and I took a time to ramble at large about all of them at once. Apologies for not taking the time to be more succinct, but at least each topic has a header so you can skip stuff you don't care about.\n\n### Maintainability\n\nIt's simply not true to say that I don't care about maintainability. I still work on the oldest Rails app in the world."
}];

export default Ember.Route.extend({
    model: function() {
        return posts;
    }
});
routes/posts.js

import Ember from 'ember';
var PostsController = Ember.ArrayController.extend();
export default PostsController;
import Ember from 'ember';


export default Ember.Mixin.create({    

    setupController: function(controller) {
        this._super(controller);
        var routeName = this.get('routeName');
        this.controllerFor('application').setActiveClass(routeName);
      }

});
import Ember from 'ember';
import ActiveclassrouteMixin from 'embercliapp/mixins/activeclassroute';

var posts = [{
    id: '1',
    title: "Rails is Omakase",
    author: {
        name: "d2h"
    },
    date: new Date('12-27-2012'),
    excerpt: "There are lots of à la carte software environments in this world. Places where in order to eat, you must first carefully look over the menu of options to order exactly what you want.",
    body: "I want this for my ORM, I want that for my template language, and let's finish it off with this routing library. Of course, you're going to have to know what you want, and you'll rarely have your horizon expanded if you always order the same thing, but there it is. It's a very popular way of consuming software.\n\nRails is not that. Rails is omakase."
}, {
    id: '2',
    title: "The Parley Letter",
    author: {
        name: "d2h"
    },
    date: new Date('12-24-2012'),
    excerpt: "My [appearance on the Ruby Rogues podcast](http://rubyrogues.com/056-rr-david-heinemeier-hansson/) recently came up for discussion again on the private Parley mailing list.",
    body: "A long list of topics were raised and I took a time to ramble at large about all of them at once. Apologies for not taking the time to be more succinct, but at least each topic has a header so you can skip stuff you don't care about.\n\n### Maintainability\n\nIt's simply not true to say that I don't care about maintainability. I still work on the oldest Rails app in the world."
}];

export default Ember.Route.extend({
    model: function() {
        return posts;
    }
});
在上述情况下,一切正常。但是,如果在扩展路由时添加使用mixin(ActiveclassrouteMixin)以及模型,则模型将停止在插座中显示:

export default Ember.Route.extend(ActiveclassrouteMixin,{
    model: function() {
        return posts;
    }
});

commond cli构建/服务过程或chrome javascript控制台中没有错误。

解决了这个问题,因此Mix-in:ActiveclassrouteMixin有一个setupController:function(controller)方法,它显然缺少模型。改成

setupController: function(controller,model) {
    this._super(controller,model);
    var routeName = this.get('routeName');
    this.controllerFor('application').setActiveClass(routeName);
  }

我将提取所需代码以:

controller.js

setupController: function(controller, models) {
  this._super(controller, models); // insure mixin.setupController() called

  controller.setProperties( models );
}
setupController: function(controller, models) {
  this._super(controller, models); // Allow others to be called
}

mixin.js

setupController: function(controller, models) {
  this._super(controller, models); // insure mixin.setupController() called

  controller.setProperties( models );
}
setupController: function(controller, models) {
  this._super(controller, models); // Allow others to be called
}

现在您可以在mixin中设置模型的属性了

如果你给我们提供
ActiveclassrouteMixin
的内容会有帮助。你把帖子移到了mixin?我不明白。这和我的答案有什么不同。请详细说明如何使用mixin在模型上设置属性?我认为由于控制器已被弃用,因此从mixin设置模型属性将更受关注。你有所有相同的代码,但我认为通过将这些代码片段组合在一起,谷歌人将有更好的机会理解是什么让这种互动起作用。