访问控制器功能中的内容 [英] Access content inside a controller function ember

查看:57
本文介绍了访问控制器功能中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个路由,控制器,这样的视图。问题是我从视图调用控制器函数 reloadTime ,但在 reloadTime 函数中我控制了这个控制器的内容,但它说未定义。我的问题是如何以ember方式访问此内容?

  App.ActivecallsRoute = Ember.Route.extend({
setupController:function(controller,model){
$ .ajax({
url:'requests / activecalls.php',
type:'POST',
success:function数据){
App.Cdrglobal.set('active_call',data.length);
controller.set('content',data);
}
})
}
});

App.ActivecallsController = Ember.ArrayController.extend({
content:[],
deleteCall:function(model){
var obj = this.findProperty 'ID',model.ID);
App.Cdrglobal.set('active_call',App.Cdrglobal.active_call-1);
this.removeObject(obj);
},
reloadTime:function(){
console.log(this.get('content')); //控制台未定义
console.log(this.content);控制台未定义
}
});


App.ActivecallsView = Ember.View.extend({
didInsertElement:function(){
this.get('controller')。reloadTime();
}
});


解决方案

您正在访问内容属性正确。你未定义的原因是因为内容属性实际上是未定义的。



现在,您的内容未定义的原因是因为Ember.js自动将控制器的内容设置为
$ b

由于您没有定义一个模型方法,返回值如果此钩子是 undefined ,因此Ember.js将控制器内容属性设置为未定义



解决方案:



创建一个虚拟模型钩子这只是返回一个空数组:

  App.ActivecallsRoute = Ember.Route.extend({
setupController:function控制器,模型){
$ .ajax({
url:'requests / activecalls.php',
type:'POST',
success:function(data){
App.Cdrglobal.set('active_call',data.length);
con troller.set(内容,数据);
}
});
},
model:function(){
return [];
}
});


I have a route, controller, view like this. The problem is I called controller function reloadTime from view but in reloadTime function I console the content for this controller but it says it is undefined. My question is how to access this content in ember?

App.ActivecallsRoute = Ember.Route.extend({
    setupController:function(controller,model){
        $.ajax({
            url:'requests/activecalls.php',
            type:'POST',
            success:function(data){
                App.Cdrglobal.set('active_call',data.length);
                controller.set('content',data);
            }
        })
    }
});

App.ActivecallsController = Ember.ArrayController.extend({
    content:[],
    deleteCall:function(model){
       var obj = this.findProperty('ID',model.ID);
       App.Cdrglobal.set('active_call',App.Cdrglobal.active_call-1);
       this.removeObject(obj);
    },
    reloadTime:function(){
        console.log(this.get('content'));//console undefined
        console.log(this.content);console undefined
    }
});


App.ActivecallsView = Ember.View.extend({
   didInsertElement:function(){
       this.get('controller').reloadTime();
   }
});

解决方案

You are accessing the content property correctly. The reason why you are getting undefined is because the content property is actually undefined.

Now the reason why your content is undefined, is because Ember.js automatically sets the controller's content to the return value of the model hook in the route.

Since you didn't define a model method, the return value if this hook is undefined and therefore Ember.js is setting the controller content property to undefined.

Solution:

Create a dummy model hook that just returns an empty array:

App.ActivecallsRoute = Ember.Route.extend({
  setupController:function(controller,model){
    $.ajax({
      url:'requests/activecalls.php',
      type:'POST',
      success:function(data){
        App.Cdrglobal.set('active_call',data.length);
        controller.set('content',data);
      }
    });
  },
  model: function() {
    return [];
  }
});

这篇关于访问控制器功能中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆