Ember.js 未捕获类型错误:对象#<;对象>;没有方法';设置';
当我尝试将我的“内容”设置为customerList时,会出现以下错误: 未捕获的TypeError:对象#对象没有方法“set” 这是我的密码:Ember.js 未捕获类型错误:对象#<;对象>;没有方法';设置';,ember.js,Ember.js,当我尝试将我的“内容”设置为customerList时,会出现以下错误: 未捕获的TypeError:对象#对象没有方法“set” 这是我的密码: App.TableController = Em.ArrayController.extend({ init: function() { this._super(); this.getData(); }, getData: function() { $.get('data/customer.json', function(
App.TableController = Em.ArrayController.extend({
init: function() {
this._super();
this.getData();
},
getData: function() {
$.get('data/customer.json', function(data) {
var customerList = Ember.A([]);
data.forEach(function(item) {
var customer = App.overv.create({
incidient: item.incidient,
customer: item.customer,
machine: item.machine,
priority: item.priority
});
customerList.pushObject(customer);
});
this.set('content', customerList);
});
}});
我也用App.router.tableController.set('content',customerList)试过,但ember再也认不出控制器了。对于1.0.0 pre 2,第二个示例非常有效。
现在我试着找出我做错了什么或可能理解错了什么。在局部变量self中保留对这一点的引用。请参阅下面的代码
App.TableController = Em.ArrayController.extend({
init: function() {
this._super();
this.getData();
},
getData: function() {
var _self = this;
$.get('data/customer.json', function(data) {
var customerList = Ember.A([]);
data.forEach(function(item) {
var customer = App.overv.create({
incidient: item.incidient,
customer: item.customer,
machine: item.machine,
priority: item.priority
});
customerList.pushObject(customer);
});
_self.set('content', customerList);
});
}});
可能只是AJAX回调上下文的问题。。。你可以试试这个:
App.TableController = Em.ArrayController.extend({
init: function() {
this._super();
this.getData();
},
getData: function() {
$.ajax({
url: 'data/customer.json',
dataType: 'json',
context: this
}).success(function(data, textStatus, jqXHR) {
var customerList = Ember.A([]);
data.forEach(function(item) {
var customer = App.overv.create({
incidient: item.incidient,
customer: item.customer,
machine: item.machine,
priority: item.priority
});
customerList.pushObject(customer);
});
this.set('content', customerList);
});
}
});
指定
TableController
作为回调的上下文。this
在中指定:this.set('content',customerList)代码>看起来不是控制器(但可能是窗口
)。如果在getData
的第一行定义:var self=this
,然后将this
替换为self
,则应该可以使用。