断言失败:即#each遍历必须是一个数组的值。您通过[对象的对象]是一个、对象、遍历、断言

由网友(趁年轻、)分享简介:我试图改变其型号为模板,在运行时,当一个按钮pssed $ P $。要更改模板被称为示例的模式,changeModel行动呼吁按钮preSS。这种方法使一个Ajax请求,并从后台得到了一些数据。现在的问题是,应用程序转换到示例模板,但新的数据不会在模板中显示。我已验证数据被从服务器发送。当我把完全相同的数据放入一个变量...

我试图改变其型号为模板,在运行时,当一个按钮pssed $ P $。要更改模板被称为示例的模式,changeModel行动呼吁按钮preSS。这种方法使一个Ajax请求,并从后台得到了一些数据。现在的问题是,应用程序转换到示例模板,但新的数据不会在模板中显示。我已验证数据被从服务器发送。当我把完全相同的数据放入一个变量,并尝试加载作为一种模式,它的工作原理。可能是什么问题?该问题是因为Ajax请求过渡到新的页面之前不解决,我需要以某种方式使用承诺?或者是数据的数据类型正在发送不同?如果是这样,我怎么可以把它转换成一个数组。这个问题是在链接到另一个问题,我问过几天回道:Rendering模板和调用模式挂钩的Ember.js 。你可以看一下code我使用。

  changeModel:功能(数据){
    VAR URL =/ ChangeModel;
    VAR newModel,并向=灰烬。$。的getJSON(URL)。然后(功能(数据){
      返回的数据;
    });
    无功自我=这一点;
    this.transitionTo(示例)。然后(函数(){
        self.controllerFor(示例)设置(模式,newModel,并向)。
    });
 }
 

解决方案

该variblenewModel,并向接缝是一个承诺。

试试这个:更换你的code:

  self.controllerFor(示例)设置(模式,newModel,并向)。
 

  newModel.then(
  功能(数据){
    self.controllerFor(示例)集(模型,数据)。
});
 
把遍历出来的数据存进对象中

I am trying to change the model for a template at runtime when a button is pressed. For changing the model of template called 'example', changeModel action is called on button press. This method makes an Ajax request and gets some data from the backend. Now the problem is that the app transitions to the example template but the new data is not displayed in the template. I verified that the data is being sent from the server. When I put exactly same data into a variable and try to load that as a model, it works. What might be the problem? Is the problem because the Ajax request is not resolved before transitioning to the new page and I need to use promise somehow? Or is the data type of data being sent different? If so, how can I convert it into an array. This question is in a way linked to the other question I asked a few days back: Rendering a template and invoking model hook in Ember.js. You can have a look at the code I am using.

changeModel: function(data) {
    var url = "/ChangeModel";
    var newModel= Ember.$.getJSON(url).then(function(data) {
      return data;
    });
    var self = this;
    this.transitionTo('example').then(function(){
        self.controllerFor('example').set('model', newModel);
    });
 }

解决方案

The varible 'newModel' seams to be a promise.

Try this: replace your code:

self.controllerFor('example').set('model', newModel);

with

newModel.then(
  function(data) {
    self.controllerFor('example').set('model', data);
});

阅读全文

相关推荐

最新文章