断言失败:即#each遍历必须是一个数组的值。您通过[对象的对象] [英] Assertion failed: The value that #each loops over must be an Array. You passed [object Object]

查看:121
本文介绍了断言失败:即#each遍历必须是一个数组的值。您通过[对象的对象]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变其型号为模板,在运行时,当一个按钮pssed $ P $。要更改模板被称为示例的模式,changeModel行动呼吁按钮preSS。这种方法使一个Ajax请求,并从后台得到了一些数据。现在的问题是,应用程序转换到示例模板,但新的数据不会在模板中显示。我已验证数据被从服务器发送。当我把完全相同的数据放入一个变量,并尝试加载作为一种模式,它的工作原理。可能是什么问题?该问题是因为Ajax请求过渡到新的页面之前不解决,我需要以某种方式使用承诺?或者是数据的数据类型正在发送不同?如果是这样,我怎么可以把它转换成一个数组。这个问题是在链接到另一个问题,我问过几天回道:<一href="http://stackoverflow.com/questions/21525243/rendering-a-template-and-invoking-model-hook-in-ember-js/21530151#21530151">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);
});

这篇关于断言失败:即#each遍历必须是一个数组的值。您通过[对象的对象]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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