1

我实现了一个可工作的待办事项应用程序,它考虑了最新 ember 版本的“查看上下文更改”(单击)。

它工作正常,但我想知道是否有更简单(更好/更短)的解决方案:http:
//jsfiddle.net/KBtwG/

(我也收到了一些恼人的未捕获错误/警告......)

模板:

<script type="text/x-handlebars">
{{view Todos.CreateTodoView placeholder="What has to be done?"}}

{{#collection contentBinding="Todos.todosController"}}
    <label>
        {{view Em.Checkbox labelBinding="content.title" checkedBinding="view.content.isDone"}}
        {{view.content.title}}
    </label>
{{/collection}}

<br /><br />

<label>
    {{view Em.Checkbox class="mark-all-done" title="Mark All as Done" checkedBinding="Todos.todosController.allAreDone"}}
    Mark All as Done 
</label>

{{#view Ember.Button target="Todos.todosController" action="clearCompletedTodos"}}
    Clear
{{/view}}

{{#view Todos.StatsView}}
    Remaining: {{view.remainingString}}
{{/view}}

​</p>

编码:

/*************************** Application ********************/
Todos = Em.Application.create();
Todos.initialize();

/*************************** Models *************************/
Todos.Todo = Em.Object.extend({
    title: null,
    isDone: false
});

/*************************** Controllers ********************/
Todos.todosController = Ember.ArrayProxy.create({
    content: [],

    createTodo: function(title) {
        var todo = Todos.Todo.create({
            title: title
        });
        this.pushObject(todo);
    },
    clearCompletedTodos: function() {
        this.filterProperty('isDone', true).forEach(this.removeObject, this);
    },
    remaining: function() {
        return this.filterProperty('isDone', false).get('length');
    }.property('@each.isDone'),
    isEmpty: function() {
        return this.get('length') === 0;
    }.property('length'),
    allAreDone: function(key, value) {
        if (arguments.length === 2) {
            this.setEach('isDone', value);

            return value;
        } else {
            return !this.get('isEmpty') && this.everyProperty('isDone', true);
        }
    }.property('@each.isDone')
});

/*************************** Views **************************/
Todos.CreateTodoView = Em.TextField.extend({
    insertNewline: function() {
        var value = this.get('value');
        if (value) {
            Todos.todosController.createTodo(value);
            this.set('value', '');
        }
    }
});

Todos.StatsView = Em.View.extend({
    remainingBinding: 'Todos.todosController.remaining',

    remainingString: function() {
        var remaining = this.get('remaining');
        return remaining + (remaining === 1 ? " item" : " items");
    }.property('remaining')
});​
4

1 回答 1

0

我已经更新了你的小提琴工作:http: //jsfiddle.net/KBtwG/2/

请注意,由于您使用的是 Ember 最新版本,因此您可以利用许多新功能,例如路由器。集成 Ember 的最新功能后,您发布的代码看起来会更好。

于 2012-09-19T14:37:00.980 回答