javascript - Ember.js 观察者在异步更新后不触发

标签 javascript jquery asynchronous ember.js

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: {

    }
});

关于javascript - Ember.js 观察者在异步更新后不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686864/

相关文章:

jquery - 如何在悬停时使用 CSS 中属性的值更改元素的背景颜色

javascript - 在 JavaScript 中通过引用从 json 树中删除对象

c# - I/O 操作的 .NET 基于事件的异步模式是否会阻塞底层线程?

c++ - 异步完成处理

javascript - Vue-动态导入vue组件

javascript - react-router-dom 仅更改 url

javascript - 在javascript中更改线条颜色

javascript - Angular 2 Routes - 是否可以使用 ngFor 从路由创建动态 routerLinks?

javascript - 如何用 php 数组值替换 jquery 变量值

c# - 检查 Task.Run 尚未运行