简体   繁体   中英

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.

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 ? 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

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()

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);
    });
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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