获取对象#<object>在Ember </object>中没有方法'map'

时间:2013-09-08 23:28:17

标签: 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.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,

<script type="text/x-handlebars" data-template-name="users">

            {{input type="text" value=searchText}}

            {{#each item in filteredContent }}
            <tr><td>
              {{#link-to 'user' item}} <p>{{item.firstname}}</p>{{/link-to}}
            </td></tr>
            {{/each}}

            {{outlet}}
        </script>


     <script type="text/x-handlebars" data-template-name="user">
    <h2>
        {{email}}
   </h2>

    </script>

我在Chrome上收到这样的错误,

Uncaught TypeError: Object #<Object> has no method 'map' 

并且Firefox给了我TypeError: data.map is not a function

这是JSON的样本,

{"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的问题。

我应该使用data.users.map代替data.map吗?但是当我使用它时,我在这一行上得到了错误,

return this.get('model').filter(function(item) {

说,

Uncaught TypeError: Object #<Object> has no method 'filter'

1 个答案:

答案 0 :(得分:3)

是的,您需要使用data.users.map,因为您在控制器的模型属性中预览了一系列元素。

Uncaught TypeError: Object #<Object> has no method 'filter'错误是因为this.get('model') data没有data.users。所以Object类没有名为filter的方法,只有Array。该数据从$.getJSON().then()方法返回,该方法使用数据解析:

$.getJSON().then(function(data) {
  // something like this is made by ember
  // controller.set('model', data);
});

你需要data.users作为ember对象。

所以我将您的代码更新为以下内容:

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。

这是显示此工作http://jsfiddle.net/marciojunior/HtJEh/

的小提琴