Javascript 获取对象#<;对象>;没有方法';地图';余烬
我有一个像这样的App.jsJavascript 获取对象#<;对象>;没有方法';地图';余烬,javascript,jquery,ember.js,Javascript,Jquery,Ember.js,我有一个像这样的App.js App = Ember.Application.create(); App.Model = Ember.Object.extend({ }); App.IndexRoute = Ember.Route.extend({ redirect : function() { this.transitionTo('users'); } }); App.UsersController = Ember.ObjectController.ex
App = Ember.Application.create();
App.Model = Ember.Object.extend({
});
App.IndexRoute = Ember.Route.extend({
redirect : function() {
this.transitionTo('users');
}
});
App.UsersController = Ember.ObjectController.extend({
filteredContent : function() {
var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');
return this.get('model').filter(function(item) {
return regex.test(item.name);
});
}.property('searchText', 'model')
});
App.User = App.Model.extend({
id : null,
firstname : null,
email : null
});
App.UsersRoute = Ember.Route.extend({
model : function() {
return App.User.findAll();
}
});
App.UserRoute = Ember.Route.extend({
model : function(params) {
//var everyone = this.controllerFor('users');
return App.User.findBy('id', params.user_id);
}
});
App.User.reopenClass({
findAll : function() {
return $.getJSON("http://pioneerdev.us/users/index", function(data) {
return data.map(function(row) {
return App.User.create(row);
});
});
}
});
App.Router.map(function() {
this.resource('users', function() {
this.resource('user',{path: '/:user_id'});
});
});
HTML
可能是什么问题?我很确定这是.getJSON的问题
我应该使用data.users.map
而不是data.map
?但是当我用这个的时候,我会在这行上出错
返回此.get('model').filter(函数(项){
说,
未捕获类型错误:对象#没有方法“过滤器”
是,您需要使用data.users.map
,因为您需要控制器的模型属性中的元素数组
uncaughttypeerror:Object没有方法“filter”
错误是因为,this.get('model')
有数据
no数据。用户
。因此Object
类没有名为filter的方法,只有数组
。该数据是从$.getJSON()返回的。然后()
使用数据解析的方法:
$.getJSON().then(function(data) {
// something like this is made by ember
// controller.set('model', data);
});
您需要data.users作为余烬对象
因此,我将您的代码更新为以下内容:
App.User.reopenClass({
findAll : function() {
return new Ember.RSVP.Promise(function(resolve, reject) {
$.getJSON("http://pioneerdev.us/users/index", function(data) {
var result = data.users.map(function(row) {
return App.User.create(row);
});
// here we resolve the array with ember objects
// so you will have this.get('model') filled with array of users
resolve(result);
}).fail(reject);
});
}
});
最好使用RSVP.Promise
,因为这是ember使用的promises api
这是显示此功能的小提琴好的,现在我有一个奇怪的问题,数据没有显示在HTML端:/Hmm奇怪。小提琴对你有用吗?我不明白你的问题,你能描述更多细节吗?我的错,这是HTML端的错误。:)谢谢帮助!:D
{"users":[{"id":"1","firstname":"Marilyn","lastname":"Hughes","address":"4 Johnson Alley","state":"Utah","country":"US","email":"lfreeman@skyvu.info","phone":"6471228888","experience":"5","designation":"Writer"},{"id":"2","firstname":"John","lastname":"Porter","address":"86228 Commercial Court","state":"Pennsylvania","country":"US","email":"jporter@zoomdog.net","phone":"9018889877","experience":"3","designation":"Design"},{"id":"3","firstname":"Frances","lastname":"Johnson","address":"489 Summit Lane","state":"Minnesota","country":"US","email":"fjohnson@teklist.net","phone":null,"experience":"2","designation":"Script"}]}
$.getJSON().then(function(data) {
// something like this is made by ember
// controller.set('model', data);
});
App.User.reopenClass({
findAll : function() {
return new Ember.RSVP.Promise(function(resolve, reject) {
$.getJSON("http://pioneerdev.us/users/index", function(data) {
var result = data.users.map(function(row) {
return App.User.create(row);
});
// here we resolve the array with ember objects
// so you will have this.get('model') filled with array of users
resolve(result);
}).fail(reject);
});
}
});