Ember,与模型一起工作 [英] Ember, working with a model

查看:93
本文介绍了Ember,与模型一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑如何从Ember.js中的(动态)模型设置检索信息

I'm confused about how to set up retrieve information from my (dynamic) model in Ember.js

这是我的模型(到目前为止): / p>

Here is my model (works so far):

App.Router.map(function() {
        this.resource('calendar', { path: '/calendar/:currentMonth'});
});

App.CalendarRoute = Ember.Route.extend({
  model: function (params) {
    var obj = {
       daysList: calendar.getDaysInMonth("2013", params.currentMonth),
       currentMonth: params.currentMonth
    };
    return obj;
  }
});

我只想收回currentMonth属性:

I just want to get back the 'currentMonth' attribute:

App.CalendarController = Ember.Controller.extend({
  next: function() {
    console.log(this.get('currentMonth'));
  }
});

但是我收到一个未定义错误。

But I am getting an "undefined" error.

为了获取和设置值,我必须明确声明我的模型(Ember.model.extend())吗?

Do I have to explicitly declare my model (Ember.model.extend()) in order to get and set values?

推荐答案

有一些您可能不知道的约定关于将模型设置为控制器

There are some conventions that you might not be aware of in regards to setting a Model into a Controller.

路由中,模型可以是您定义的任何对象或对象集合。有大量的约定适用,在大多数情况下,您不必指定任何东西,因为它使用各种对象的名称来指导自己构建查询和设置控制器的内容,但是在您的特定代码,您将返回 obj 作为模型。

In a Route, model can be any object or collection of objects you define. There is a huge deal of conventions that apply and for most cases, you don't have to specify anything as it uses the names of various objects to guide itself on building the query and a set the content of your controller, however, in your particular code, you are return obj as the model.

Ember提供了一个名为 setupController 的钩子,将此对象设置为您的控制器的内容属性。示例:

Ember provides a hook called setupController that will set this object into your controller's content property. Example:

App.CalendarRoute = Ember.Route.extend({
  model: function (params) {
    var obj = {
       daysList: calendar.getDaysInMonth("2013", params.currentMonth),
       currentMonth: params.currentMonth
    };
    return obj;
  },
  setupController: function(controller, model) {
     // model in this case, should be the instance of your "obj" from "model" above
     controller.set('content', model);
  }
});

说到这里,你应该尝试 console.log(this.get 'content.currentMonth'));

With that said, you should try console.log(this.get('content.currentMonth'));

这篇关于Ember,与模型一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆