0

When i try set my 'content' to the customerList i'll get the error:

Uncaught TypeError: Object #Object has no method 'set'

Here is my code:

  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);

    });
}});

I I also tried it with App.router.tableController.set('content',customerList) but ember doesn't recognize the controller anymore. With 1.0.0 pre 2 this second example worked great. Now i try to figure out what i did wrong or probably understood wrong.


Keep a reference of this in a local variable _self. See code below.

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);

    });
}});
4

2 回答 2

1

probably just an issue with the context of the AJAX callback... you could try this:

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);
      });
  }
});

specifying TableController as the context to the callback.

于 2013-01-24T20:47:44.777 回答
1

Keep a reference of this in a local variable _self. See code below.

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);

    });
}});
于 2013-01-24T20:42:17.777 回答