0

我正在尝试创建“排序依据”按钮,当单击使用 Ember.js 时更改排序和 css 类。

排序部分和初始类分配工作,但是,当我更新依赖属性时,类分配不会刷新。

我错过了什么?

这是在我的 HTML 中:

<script type="text/x-handlebars" data-template-name="sort-option-item">
    <dd {{bindAttr class="IsActive:active IsDesc:reversed"}}
        {{action sortBy on="click"}}><a href="#">{{Name}}</a></dd>
</script>

<script type="text/x-handlebars">
    {{#each option in controller.sortOptions}}
        {{view App.SortOptionView controllerBinding="option"}}
    {{/each}}
</script>

这是在我的 Javascript 中:

var App = null;

$(function () {

    App = Ember.Application.create();

    // Define Types:

    App.SortOptionCtrl = Ember.Controller.extend({
        Name: null,
        Predicate: null,
        Controller: null,
        IsActive: false,
        IsDesc: false,
        sortBy: function () {
            if (this.Controller != null)
                this.Controller.sortBy(this.Predicate);
        },
        Check: function () {
            this.IsActive = this.Controller != null
                            && this.Controller.isSortedBy(this.Predicate);
            this.IsDesc = this.Controller != null
                          && this.Controller.isSortedDescBy(this.Predicate);
            // adding an alert(this.IsActive); here
            // proves that the function is indeed called and works as expected
        }
    });

    App.ProductsController = Ember.ArrayController.extend({

        initialized: false,
        content: [],
        viewContent: [],
        sortProperties: ['Order'],
        sortAscending: true,
        sortOptions: [],

        initialize: function () {

            if (this.initialized == true)
                return;

            this.initialized = true;
            var ctrl = this;

            this.sortOptions.pushObject(App.SortOptionCtrl.create({
                Name: 'Unsorted',
                Predicate: null,
                Controller: ctrl,
            }));
            this.sortOptions.pushObject(App.SortOptionCtrl.create({
                Name: 'By Name',
                Predicate: 'Name',
                Controller: ctrl,
            }));
            this.sortOptions.pushObject(App.SortOptionCtrl.create({
                Name: 'By Date',
                Predicate: 'Date',
                Controller: ctrl,
            }));

            this.sortOptions.forEach(function (opt) { opt.Check(); });
        },

        load: function () {

            this.initialize();
            // ....
        },

        sortBy: function (predicate) {

            var prevPredicate = this.sortProperties[0];

            if (predicate == prevPredicate && predicate != null) {
                this.sortAscending = !(this.sortAscending);
            }
            else {
                this.sortAscending = true;
            }

            this.sortProperties.length = 0;

            if (predicate)
                this.sortProperties.pushObject(predicate);
            else
                this.sortProperties.pushObject('Order');

            this.sortOptions.forEach(function (opt) { opt.Check(); });
        },

        isSortedBy: function (predicate) 
        {
            if (predicate == null)
                predicate = 'Order';

            var activePredicate = this.sortProperties[0];

            if (predicate == activePredicate) {
                return true;
            }
            else {
                return false;
            }
        },

        isSortedDescBy: function (predicate) {

            if (predicate == null)
                predicate = 'Order';

            var activePredicate = this.sortProperties[0];

            if (predicate == activePredicate) {
                if (this.sortAscending)
                    return false;
                else
                    return true;
            }
            else {
                return false;
            }
        },

    });

    App.SortOptionView = Ember.View.extend({
        templateName: 'sort-option-item'
    });

    // Create Instances:

    App.productsController = App.ProductsController.create({
    });

    App.productsController.load();

    App.initialize();
});

版本:Ember:1.0.0-rc.2,车把:1.0.0-rc.3

4

2 回答 2

2

如果您希望您的视图对控制器中发生的任何事情做出反应,您应该创建计算属性(通过fn(){}.property('dependency'))。但是,为了使计算属性正常工作,您需要使用 Emberget()set()属性访问器。

在您的代码中,您正在做类似的事情

this.IsActive = this.Controller != null && 
                this.Controller.isSortedBy(this.Predicate);

当你应该做这样的事情时:

this.set('active',
     this.get('controller') != null &&
     this.get('controller').isSortedBy(this.get('Predicate'))
);

您可能已经注意到,这段代码将值设置active,但模板正在侦听isActive。该属性已更改为计算属性:

isActive: function() {
    return this.get('active');
}.property('active')

它将监听active属性的变化,每当发生这种情况时,它将缓存新值,并通知所有订阅者对象刷新/更新。

指示使用 Embergetset访问器是为了正确使用使此事件链成为可能的可观察对象。

我已经修改了您的示例应用程序getset在适当的情况下进行了修改。你可以在这个小提琴中看到它:http: //jsfiddle.net/schawaska/fRMYu/

于 2013-04-13T22:22:04.757 回答
0

在乔的帮助下,更多地阅读了 Ember 的手册并清理了我的代码,我得到了这个分拣机解决方案:

排序选项控制器:

App.SortOptionCtrl = Em.Controller.extend({
    Name: null,
    Predicate: null,

    _isActive: false,
    isActive: function () {
        return this.get('_isActive');
    }.property('_isActive'),

    _isDesc: false,
    isDesc: function () {
        return this.get('_isDesc');
    }.property('_isDesc'),

    controller: null,

    updateState: function () {
        if (!this.Predicate) {
            this.set('_isActive', (this.get('controller')
                .get('activePredicate') == null));
            this.set('_isDesc', false);
        }
        else {
            this.set('_isActive', (this.get('controller')
                .get('activePredicate') == this.Predicate));
            this.set('_isDesc', (this.get('_isActive')
                && !this.get('controller').get('sortAscending')));
        }
    }.observes('controller.activePredicate', 'controller.sortAscending'),

    sortBy: function () {
        if (this.get('controller') != null) {
            this.get('controller').sortBy(this.Predicate);
        }
    },
});

产品控制器:

App.ProductsController = Ember.ArrayController.extend({

    content: [],
    viewContent: [],
    activePredicate: null,
    sortProperties: ['Order'],
    sortAscending: true,
    sortOptions: [],

    filter: function (obj) {
        return true;
    },

    init: function () {

        this._super();

        var ctrl = this;

        this.sortOptions.pushObject(App.SortOptionCtrl.create({
            Name: 'Unsorted',
            Predicate: null,
            controller: ctrl,
        }));
        this.sortOptions.pushObject(App.SortOptionCtrl.create({
            Name: 'By Name',
            Predicate: 'Name',
            controller: ctrl,
        }));
        this.sortOptions.pushObject(App.SortOptionCtrl.create({
            Name: 'By Date',
            Predicate: 'Date',
            controller: ctrl,
        }));

        this.sortOptions.forEach(function (opt) {
            opt.updateState();
        });
    },

    sortBy: function (predicate) {

        var prevPredicate = this.sortProperties[0];

        if (predicate == prevPredicate && predicate != null) {
            this.set('sortAscending', !(this.get('sortAscending')));
        }
        else {
            this.set('sortAscending', true);
        }

        this.set('activePredicate', predicate);
        this.set('sortProperties.length', 0);

        if (predicate)
            this.get('sortProperties').pushObject(predicate);
        else
            this.get('sortProperties').pushObject('Order');
    },

});
于 2013-04-24T20:16:30.377 回答