简体   繁体   English

Ember.js:使用没有路由的ArrayController吗?

[英]Ember.js: Using an ArrayController without a route?

I have a list of categories that I would like to display on the page. 我有一个要显示在页面上的类别列表。 I've been looking around for hours on the correct way to do this, but I keep finding conflicting information and methods that are either no longer working with the current version of Ember, or are deprecated. 我已经花了几个小时寻找正确的方法,但是我一直在寻找冲突的信息和方法,这些信息和方法要么不再与当前版本的Ember一起使用,要么已弃用。

To create the app and Ember-Data thing: 要创建应用程序和Ember-Data事物:

var App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter;

App.Store = DS.Store.extend({
  adapter: 'DS.FixtureAdapter'
});

I am using Ember-Data with some fixture data for the categories: 我正在将Ember-Data与一些灯具数据用于类别:

App.Category.FIXTURES = [{
  id:          1,
  name:        'Category 1',
  description: 'The first category'
}, {
  id:          2,
  name:        'Category 2',
  description: 'The second category'
}];

I then create a model to represent an instance of a category (the "versions" relates to a different model where version instances have category instances): 然后,我创建一个模型来表示类别的实例(“版本”与版本实例具有类别实例的其他模型相关):

App.Category = DS.Model.extend({
  versions:    DS.hasMany('version'),
  name:        DS.attr('string'),
  description: DS.attr('string')
});

Then I made an ArrayController, but I am having trouble populating the content. 然后,我做了一个ArrayController,但是在填充内容时遇到了麻烦。

App.CatagoriesController = Ember.ArrayController.create({
  content: [],
  init: function () {
    this.set('content', ???);
  }
});

This isn't hooked up to a route since it isn't navigable, so I can't use the "model" property in a route. 由于它不可导航,因此未连接到路由,因此无法在路由中使用“ model”属性。

Replacing the ??? 更换??? in the ArrayController, I've tried several things: 在ArrayController中,我尝试了几件事:

I tried this.store.find('category') like the examples show for routes, but I get the error "Uncaught TypeError: Cannot read property 'find' of null". 我尝试了this.store.find('category')如路线示例所示,但是出现错误“ Uncaught TypeError:无法读取null的属性'find'”。

I tried App.Store.find('category') and that gave me the error "Uncaught TypeError: undefined is not a function". 我尝试了App.Store.find('category') ,这给了我错误“未捕获的TypeError:未定义不是函数”。

Any help or a push in the right direction is appreciated. 任何帮助或朝着正确方向的推动都将受到赞赏。 I have a feeling I'm not even headed in the right direction. 我感觉自己甚至没有朝着正确的方向前进。

A couple of things. 有几件事。

First, remember that controllers are singletons. 首先,请记住,控制器是单例的。 So your init function will only fire once for the entire lifecycle of your application (which probably isn't what you want). 因此,您的init函数在应用程序的整个生命周期中只会触发一次(这可能不是您想要的)。 But that doesn't really matter, since... 但这并不重要,因为...

The way that you're handling models isn't the way that Ember.js is set up for. 处理模型的方式不是Ember.js的设置方式。 You shouldn't be fetching models in controllers, only routes. 您不应该仅在路由中获取控制器中的模型。 That's why there is no store property on the controller. 这就是为什么控制器上没有store属性的原因。 You need to implement the model hook on your route, like this: 您需要在路线上实现model挂钩,如下所示:

App.CategoriesRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('category');
    }
});

From there, Ember will automatically set the model property ( not the content property, don't use that) on your controller to the data that you return from the model hook. 从那里,Ember将自动在控制器上将您从model挂钩返回的数据设置为model属性( 不是 content属性,不要使用该属性)。

So, just eliminate the content property and init function from your controller, and implement that hook in your model, and you should be good to go. 因此,只需从控制器中删除content属性和init函数,并在模型中实现该钩子,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM