Ember:在promise中设置组件对象值

时间:2016-05-26 08:12:30

标签: ember.js

我有一些带有几个属性的组件,使用willRender钩子中的promise来尝试创建一个(分页)对象:

export default Ember.Component.extend({
   pagination:null,
   testing:null, // to check if this.set is ok within the promise!
   willRender() {
      let page = {};
      let model = this.get('data');
      model.get('products').then(relatedItems => {    
          let maxRecords = relatedItems.get('length');
          relatedItems.forEach(function(item,index) {
              if (item.get('slug') === itemModel.get('id')) {
                if (index === 0) {
                  page.Prev = null;
                  page.Next = relatedItems.objectAt(index+1).get('slug');
                }
               else if (index+1 === maxRecords) {
                 page.Prev = relatedItems.objectAt(index-1).get('slug');
                 page.Next = null;
               }
            else {
               page.Prev = relatedItems.objectAt(index-1).get('slug');
               page.Next = relatedItems.objectAt(index+1).get('slug');
             }
            }
          });
          this.set('testing','hello world');
          console.log(this.get('testing')); // hello world
          this.set('pagination',page);
          console.log(this.get('pagination')); // Object {Prev: "product-1", Next: "product-2"}

      },reject => {
          console.log('error '+reject);
      });
   }
})

在我的模板中

{{testing}} // prints hello world

但是,如果我尝试访问{{pagination}}例如{{log pagination}},则浏览器会崩溃并将对象打印出来并将对象打印到控制台。

我不知道我在哪里出错 - 任何帮助都非常感谢!

5 个答案:

答案 0 :(得分:0)

尝试检查对象是否存在于特定位置。我认为它在for循环的迭代期间未定义。尝试确保在运行for循环期间ObjectAt没有返回undefined或null值。

<?php
    //simply api
    if(isset($_POST["submitmail"])){
        if(strlen($_POST["mail"]) >= 5){
            $api = file_get_contents("API_LINK");

            if($api == "good"){
                echo "<script>alert('Thanks');</script>";    
            }
        }else{
            echo "<script>alert('Try again');</script>";
        }
    }
?>

请确保Object at正在返回对象。

答案 1 :(得分:0)

您可能会触发模板重新渲染,导致willRender一遍又一遍地触发,导致代码无限循环。

willRender是执行此代码的非标准位置,init将更加标准,因为它仅在组件初始化时触发。更好的是使用

myInit: Ember.on('init', function(){ 
    .... 
})` 

而不是覆盖对象上的willRender

答案 2 :(得分:0)

这里似乎有一些问题,有兴趣知道你的控制台错误是什么。

您似乎没有定义itemModel,所以不知道您是如何引用它的。

此外,您无法在this内访问.then。您需要执行类似的操作来设置组件变量。

var _this = this;

promise().then(function(results) {
  _this.set('testing', 'hello world');
});

答案 3 :(得分:-1)

<div data-role="popup" id="popup-info" data-theme="b" class="ui-content" > <a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-left">Fermer</a> <div id="content"></div> </div> <script> (function($) { $.widget( "mobile.popup", $.mobile.popup, { setText: function(_text) { container=this.element.find('div:first'); if(container!==undefined){ container.html(_text); }else{ this.element.html(_text); } var newX = parseInt(($(window).width()-this._ui.container.width())/2); var newY = parseInt(($(window).height()-this._ui.container.height())/2); this.reposition( {x:newX,y:newY,positionTo:'window'} ); } }); })(jQuery); $('#popup-info').popup('setText',str); </script> 之后你没有使用, 在测试属性之后应该testing:null

,

答案 4 :(得分:-1)

尝试在init挂钩而不是willrender挂钩

下使用您的分页代码
init() {
    //you code
  },
相关问题