簡體   English   中英

Ember.js觀察者在異步更新后沒有觸發

[英]Ember.js observer not firing after asynchronous update

App.BaseRoute = App.AuthenticatedRoute.extend({
    setupController: function(controller, model){
        var cuser = this.controllerFor('application').get('currentUser');
        controller.set('model',model);
        this.controllerFor('base').set("currTags",App.User.getUserTags(cuser._id.$oid));
    }
});

App.IndexRoute = App.BaseRoute.extend({
    model: function(params) {
        return {"adsf" :"blah"};
    },  
    setupController: function(controller, model){
        this._super(controller,model);
        this.controllerFor('posts').set('model',model);
    }
});

App.User.reopenClass({
    getUserTags: function(userid) {
        var currTags = []; //create an empty object
        currTags.set('isLoaded',false);
        console.log(currTags);
        $.ajax({
            type: "GET",
            url : "/user/"+userid+"/default_tags",
            dataType: "application/json",
            //contentType : "application/json"
        }).then(this,function(data){
            data = JSON.parse(data.responseText);

            data.models.tags.forEach(function(tag){
                var model = App.Tag.create(tag); 
                model.value = model.name;
                currTags.addObject(model); //fill your array step by step

            });
            console.log(currTags);
            currTags.set("isLoaded", true);

        });
        console.log(currTags);
        return currTags;
    }
});

App.IndexController = Ember.ArrayController.extend({
    needs: ['application','base' ,'posts'],
    currentUser: Ember.computed.alias("controllers.application.currentUser"),
    posts : Ember.computed.alias("controllers.posts"),
    currTags: Ember.computed.alias("controllers.base.currTags"),
    actions: {
        initCurrentTags: function() {

        }.observes('currTags.isLoaded')
    }
});

我更新currTags但initCurrentTags永遠不會被擊中。

計算屬性,並且觀察不應該存在於動作哈希中。

不正確

App.IndexController = Ember.ArrayController.extend({
    needs: ['application','base' ,'posts'],
    currentUser: Ember.computed.alias("controllers.application.currentUser"),
    posts : Ember.computed.alias("controllers.posts"),
    currTags: Ember.computed.alias("controllers.base.currTags"),
    actions: {
        initCurrentTags: function() {

        }.observes('currTags.isLoaded')
    }
});

正確

App.IndexController = Ember.ArrayController.extend({
    needs: ['application','base' ,'posts'],
    currentUser: Ember.computed.alias("controllers.application.currentUser"),
    posts : Ember.computed.alias("controllers.posts"),
    currTags: Ember.computed.alias("controllers.base.currTags"),
    initCurrentTags: function() {

    }.observes('currTags.isLoaded')

    actions: {

    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM