Ember.js 对数组排序不会更新Dom

Ember.js 对数组排序不会更新Dom,ember.js,Ember.js,我无法在我的测试应用程序中使用以下代码: this.propertyWillChange('tableContent'); this.get('tableContent').sort(function (a, b) { var nameA = a.artikel_name, nameB = b.artikel_name; if (nameA < nameB) { return -1; } if (nameA > nam

我无法在我的测试应用程序中使用以下代码:

this.propertyWillChange('tableContent');
this.get('tableContent').sort(function (a, b) {
     var nameA = a.artikel_name,
     nameB = b.artikel_name;
     if (nameA < nameB) {
        return -1;
     }
     if (nameA > nameB) {
        return 1;
      }
      return 0 //default return value (no sorting)
  });
 this.propertyDidChange('tableContent');

如果复制数组并重新分配复制的数组,同样的代码也会起作用。

是否尝试使用内置的SortableMixin?如果没有,这对你有好处吗

JavaScript:

App = Ember.Application.create();

App.activities = Ember.ArrayController.create({
  content: [{name: 'sleeping'}, {name: 'eating pizza'},
            {name: 'programming'}, {name: 'looking at lolcats'}],
  sortProperties: ['name']
});

App.ActivityView = Ember.View.extend({
  tagName: "li",
  template: Ember.Handlebars.compile("{{content}}")
});

App.SortButton = Ember.View.extend({
  tagName: "button",
  template: Ember.Handlebars.compile("Sort"),
  click: function() {
    App.activities.toggleProperty('sortAscending');
  }
});

jsiddle:

您能提供一个jsiddle吗?模拟AJAX调用是可能的,所以这个例子应该可以用小提琴来演示…-但是我没有可用的公共数据来模拟ajax调用。@MilkyWayJoe是的,但我会等rogergl看看它是否适用于他的应用:)我不知道这是否适用于他的特定实现,因为他正在使用一个属性来观察集合中的更改,但该属性正在更改为相同的值“newOrder”。单击该按钮,它将在控制器中触发
sort
方法,但它从未被重新设置,因此在我看来,
sort
方法仅触发一次,因为下次更改该属性的值时,它将与以前的值相同。我错了吗?我认为他应该使用你的实现,看起来更干净,并且解决了工作问题#每个NewApp.router.gridController.tableContent都不工作,#每个NewApp.router.gridController.content但#每个NewApp.router.gridController都工作。由于上一条注释不完整:问题已解决#每个NewApp.router.gridController.tableContent都不工作,#每个NewApp.router.gridController.content都不工作,但#每个NewApp.router.gridController都工作@我使用.notifyPropertyChange使排序方法重复工作。但是toggleProperty确实更干净。您是否使用了
sortProperties
属性?换句话说,您是否尝试过使用ArrayController中的内置排序功能而不是您的?
selectionChanged: function () {
   var that = this;
   if (this.selection) {
    $.getJSON("api/v1/lists/" + this.selection.id + "/prices", function (content) {
        that.set('tableContent', content);
    });
}
}.observes('selection')
App = Ember.Application.create();

App.activities = Ember.ArrayController.create({
  content: [{name: 'sleeping'}, {name: 'eating pizza'},
            {name: 'programming'}, {name: 'looking at lolcats'}],
  sortProperties: ['name']
});

App.ActivityView = Ember.View.extend({
  tagName: "li",
  template: Ember.Handlebars.compile("{{content}}")
});

App.SortButton = Ember.View.extend({
  tagName: "button",
  template: Ember.Handlebars.compile("Sort"),
  click: function() {
    App.activities.toggleProperty('sortAscending');
  }
});