本文介绍了Emberjs 1.0.0-RC3:使用NEEDS-API访问其他控制器内容属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一页面上呈现事件和约会。但是当我使用 AppointmentController 中的需求api 可以访问 EventsController 时,我可以看到它在中返回约会事件控制器内容属性。但是,当我尝试使用包含'约会'的返回内容来取出约会时,它将失败。我正在使用 eventAppt = this.get('controllers.events')获取 AppintmentsController 中的事件内容。获取内容和返回的内容的get('model'); '约会'作为其数据的一部分,但是当我尝试使用以下方式获取返回的内容数据中的约会列表: eventAppt.get('约会); this.set('content',myAppoint); ,它总是失败,而不能调用方法'indexOf'为未定义的

I want to render events and appointments on thesame page. But when I use the needs api from the AppointmentController to have access to EventsController, I can see that it returns appointments inside the 'events controller content property' when inspected. But when i try to use that returned content that contains 'appointments' to fetch out the appointments it fails. I am fetching events content in AppintmentsController with eventAppt = this.get('controllers.events').get('model'); which fetches the content and this returned content has 'appointments' as part of its data, but when I try to get the list of appointments inside the returned content's data using: eventAppt.get('appointments'); this.set('content', myAppoint);, it always fails with Cannot call method 'indexOf' of undefined.

   App.AppointmentsController = Em.ArrayController.extend({
     needs: ['events'],

     appoint: function(){
       console.log("this: ", this.toString());

       var eventAppt = this.get('controllers.events').get('model');

        //This returns the correct data that includes 'appointments'
       console.log(eventAppt);

       var myAppoint=  eventAppt.get('appointments');

       this.set('content', myAppoint);

       //This fails with 'Cannot call method 'indexOf' of undefined'
       console.log(this.get(myAppoint));
     }
  });

EventController

The EventController

 App.EventsController = Em.ArrayController.extend({

});

模型:

App.Event = DS.Model.extend({
  title: DS.attr('string'),
  start: DS.attr('date'),
  allDay: DS.attr('boolean'),
  appointments: DS.hasMany('App.Appointment')
});

有没有人知道为什么这不工作。

Does any one have an idea why this is not working.

推荐答案

在更好地了解你的代码之后,很明显为什么你会收到错误。让我试着解释一下。当你做:

After looking better at your code it's obvious why you are getting the error. Let me try to explain. When you do:

var eventAppt = this.get('controllers.events').get('model');

这将返回一个事件数组这个数组的元素是你的约会的地方。因此,执行以下操作:

this returns an array of eventsand inside this array's elements is where your appointments are. Therefore doing:

//does not work because eventAppt is an array
var myAppoint=  eventAppt.get('appointments');

正确地抛出一个错误,因为你正在绑定约会事件数组,有意义吗?

throws correctly an error because you are tying to get appointments from an array of events, makes sense?

所以,如果你需要所有的$ $ c>约会住在你的事件模型中,你应该循环你的事件内容并提取约会,或者如果您只想要一个特定的约会,或者更喜欢这样的事情:

So, if you need all the appointments living inside your event models then you should loop over your events content and extract the appointments, or rather something like this if you only want one specific appointment:

var eventAppt = this.get('controllers.events').get('model');
eventAppt.objectAt(1).get('appointments') // this prints correctly the appointments of the first event in the array of events.

我希望这样可以使事情变得更加清晰。

I hope this makes things more clear now.

这篇关于Emberjs 1.0.0-RC3:使用NEEDS-API访问其他控制器内容属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:27