简体   繁体   English

Emberjs-承诺的setProperties

[英]Emberjs - setProperties from a Promise

I'm trying to set a variable in my router from the inside of a promise, but it seems that it is not set properly. 我正在尝试从Promise的内部在路由器中设置变量,但似乎未正确设置。

I have create a small example to show you : 我创建了一个小示例向您展示:

App.ApplicationRoute = Ember.Route.extend({
    myVar: null,

    model: function() {
        var data = getData();
        console.log(myVar); // = null
        return data;
    },
    getData: function() {
        var self = this;
        return new Ember.RSVP.Promise( function(resolve, reject) {
            self.setProperties({ myVar: 42 });

            resolve(someGoodStuff);

            reject(someBadStuff)
        });
    }
})

When I try to display myVar, it still at null even when it was waiting for the promise to resolve... Do I need to do something special ? 当我尝试显示myVar时,即使它正在等待诺言解决,它仍然为null ...我需要做一些特别的事情吗? or I'm doing it wrongly ? 还是我做错了?

I believe you are looking for something like this: http://emberjs.jsbin.com/gosoj/3/edit?html,css,js,output 我相信您正在寻找这样的东西: http : //emberjs.jsbin.com/gosoj/3/edit?html,css,js,output

I'm not entirely sure what you are going for here, so there is also some other commented code that may have been your intention. 我不完全确定您要在这里做什么,因此还有一些其他注释的代码可能是您的意图。

I think maybe the problem was when you were console logging, as well as the fact that you weren't using your getters and setters properly, and the way you were calling getData() 我认为问题可能出在您何时进行控制台日志记录,以及您未正确使用getter和setter以及调用getData()的方式这一事实。

Here is the code: 这是代码:

App.ApplicationRoute = Ember.Route.extend({
  myVar: null,

  model: function() {
    var data = this.getData();
    console.log(data);
    console.log(this.get('myVar')); // = null
    return data;
  },
  getData: function() {
    var self = this;
    return new Ember.RSVP.Promise( function(resolve, reject) {
        self.setProperties({ myVar: 42 });
        resolve(42);

        reject(reason);
    }).then(function (value) {
        //self.setProperties({ myVar: value });
        console.log(self.get('myVar'));
    }, function(reason){
      console.log(reason);
    });
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM