Commit 428dc1ae authored by Stas SUȘCOV's avatar Stas SUȘCOV

Move some of the views to separate files.

Initialize controller views upon `init`.
parent ea6780df
define('app/controllers/todos', [ define('app/controllers/todos', [
'app/controllers/entries', 'app/controllers/entries',
'text!app/views/clear_button.html', 'app/views/items',
'text!app/views/items.html' 'app/views/stats',
'app/views/filters',
'app/views/clear_button',
], ],
/** /**
* Todos controller * Todos controller
...@@ -10,75 +12,42 @@ define('app/controllers/todos', [ ...@@ -10,75 +12,42 @@ define('app/controllers/todos', [
* which is an `ArrayProxy` linked with the `Store` model * which is an `ArrayProxy` linked with the `Store` model
* *
* @param Class Entries, the Entries class * @param Class Entries, the Entries class
* @param String button_html, the html view for the clearCompletedButton * @param Class ItemsView, items view class
* @param String items_html, the html view for the `Todos` items * @param Class StatsView, stats view class
* @param Class FiltersView, filters view class
* @param Class ClearBtnView, clear button view class
* @returns Class * @returns Class
*/ */
function( Entries, button_html, items_html ) { function( Entries, ItemsView, StatsView, FiltersView, ClearBtnView ) {
return Entries.extend({ return Entries.extend({
// New todo input // New todo input
inputView: Ember.TextField.create({ InputView: Ember.TextField.extend({
placeholder: 'What needs to be done?', placeholder: 'What needs to be done?',
elementId: 'new-todo', elementId: 'new-todo',
storageBinding: 'Todos.todosController',
// Bind this to newly inserted line // Bind this to newly inserted line
insertNewline: function() { insertNewline: function() {
var value = this.get( 'value' ); var value = this.get( 'value' );
if ( value ) { if ( value ) {
this.get( 'storage' ).createNew( value ); this.get( 'controller' ).createNew( value );
this.set( 'value', '' ); this.set( 'value', '' );
} }
} }
}), }),
// Stats report
statsView: Ember.View.create({
elementId: 'todo-count',
tagName: 'span',
contentBinding: 'Todos.todosController',
remainingBinding: 'Todos.todosController.remaining',
template: Ember.Handlebars.compile(
'<strong>{{remaining}}</strong> {{remainingString}} left'
),
remainingString: function() {
var remaining = this.get( 'remaining' );
return ( remaining === 1 ? ' item' : ' items' );
}.property( 'remaining' )
}),
// Handle visibility of some elements as items totals change // Handle visibility of some elements as items totals change
visibilityObserver: function() { visibilityObserver: function() {
$( '#main, #footer' ).toggle( !!this.get( 'total' ) ); $( '#main, #footer' ).toggle( !!this.get( 'total' ) );
}.observes( 'total' ), }.observes( 'total' ),
// Clear completed tasks button
clearCompletedButton: Ember.Button.create({
template: Ember.Handlebars.compile( button_html ),
target: 'Todos.todosController',
action: 'clearCompleted',
completedCountBinding: 'Todos.todosController.completed',
elementId: 'clear-completed',
classNameBindings: 'buttonClass',
// Observer to update class if completed value changes
buttonClass: function () {
if ( !this.get( 'completedCount' ) )
return 'hidden';
}.property( 'completedCount' )
}),
// Checkbox to mark all todos done. // Checkbox to mark all todos done.
allDoneCheckbox: Ember.Checkbox.create({ MarkAllChkbox: Ember.Checkbox.extend({
elementId: 'toggle-all', elementId: 'toggle-all',
checkedBinding: 'Todos.todosController.allAreDone' checkedBinding: 'controller.allAreDone'
}),
// Compile and render the todos view
todosView: Ember.View.create({
template: Ember.Handlebars.compile( items_html )
}), }),
// Todo list item view // Todo list item view
todoView: Ember.View.extend({ ItemView: Ember.View.extend({
contentBinding: 'controller',
classNames: [ 'view' ], classNames: [ 'view' ],
doubleClick: function() { doubleClick: function() {
this.get( 'content' ).set( 'editing', true ); this.get( 'content' ).set( 'editing', true );
...@@ -86,8 +55,8 @@ define('app/controllers/todos', [ ...@@ -86,8 +55,8 @@ define('app/controllers/todos', [
}), }),
// Todo list item editing view // Todo list item editing view
todoEditor: Ember.TextField.extend({ ItemEditor: Ember.TextField.extend({
storageBinding: 'Todos.todosController', storageBinding: 'content',
classNames: [ 'edit' ], classNames: [ 'edit' ],
whenDone: function() { whenDone: function() {
this.get( 'todo' ).set( 'editing', false ); this.get( 'todo' ).set( 'editing', false );
...@@ -109,11 +78,20 @@ define('app/controllers/todos', [ ...@@ -109,11 +78,20 @@ define('app/controllers/todos', [
// Activates the views and other initializations // Activates the views and other initializations
init: function() { init: function() {
this._super(); this._super();
this.set( 'inputView', this.InputView.create({ controller: this }) );
this.set( 'markAllChkbox', this.MarkAllChkbox.create({ controller: this }) );
this.set( 'itemsView', ItemsView.create({ controller: this }) );
this.set( 'statsView', StatsView.create({ controller: this }) );
this.set( 'filtersView', FiltersView.create() );
this.set( 'clearBtnView', ClearBtnView.create({ controller: this }) );
this.get( 'inputView' ).appendTo( 'header' ); this.get( 'inputView' ).appendTo( 'header' );
this.get( 'allDoneCheckbox' ).appendTo( '#main' ); this.get( 'markAllChkbox' ).appendTo( '#main' );
this.get( 'todosView' ).appendTo( '#main' ); this.get( 'itemsView' ).appendTo( '#main' );
this.get( 'statsView' ).appendTo( '#footer' ); this.get( 'statsView' ).appendTo( '#footer' );
this.get( 'clearCompletedButton' ).appendTo( '#footer' ); this.get( 'clearBtnView' ).appendTo( '#footer' );
this.get( 'filtersView' ).appendTo( '#footer' );
} }
}); });
} }
......
<ul id="filters">
<li>
<a {{action showAll href=true}}>All</a>
</li>
<li>
<a {{action showActive href=true}}>Active</a>
</li>
<li>
<a {{action showCompleted href=true}}>Completed</a>
</li>
</ul>
{{#collection id="todo-list" contentBinding="Todos.todosController" tagName="ul" itemClassBinding="content.completed content.editing" }} {{#collection contentBinding="controller" controllerBinding="controller" id="todo-list" tagName="ul" itemClassBinding="content.completed content.editing" }}
{{#unless content.editing}} {{#unless content.editing}}
{{#view Todos.todosController.todoView contentBinding="content" }} {{#view controller.ItemView contentBinding="content" }}
{{view Ember.Checkbox valueBinding="content.completed" class="toggle"}} {{view Ember.Checkbox valueBinding="content.completed" class="toggle"}}
{{#view Ember.View tagName="label" todoBinding="content"}} {{#view Ember.View tagName="label" todoBinding="content"}}
{{todo.title}} {{todo.title}}
{{/view}} {{/view}}
{{view Ember.Button target="Todos.todosController" action="removeObject" class="destroy" todoBinding="content"}} {{view Ember.Button target="controller" action="removeObject" class="destroy" todoBinding="content"}}
{{/view}} {{/view}}
{{else}} {{else}}
{{view Todos.todosController.todoEditor todoBinding="content" valueBinding="content.title"}} {{view controller.ItemEditor todoBinding="content" valueBinding="content.title"}}
{{/unless}} {{/unless}}
{{/collection}} {{/collection}}
{{#if oneLeft }}
<strong>{{remaining}}</strong> item left
{{else}}
<strong>{{remaining}}</strong> items left
{{/if}}
Clear completed ({{completedCount}})
\ No newline at end of file
define('app/views/clear_button', [
'text!app/templates/clear_button.html',
'ember'
],
/**
* View to clear completed tasks
*
* @param String button_html, the html for view
* @returns Class
*/
function( button_html ) {
return Ember.Button.extend({
template: Ember.Handlebars.compile( button_html ),
target: 'controller',
action: 'clearCompleted',
completedBinding: 'controller.completed',
elementId: 'clear-completed',
classNameBindings: 'buttonClass',
// Observer to update class if completed value changes
buttonClass: function () {
if ( !this.get( 'completed' ) )
return 'hidden';
}.property( 'completed' )
})
}
);
define('app/views/filters', [
'text!app/templates/filters.html',
'ember'
],
/**
* View to render filter links
*
* @param String filters_html, filter links html view
* @returns Class
*/
function( filters_html ) {
return Ember.View.extend({
template: Ember.Handlebars.compile( filters_html )
})
}
);
define('app/views/items', [
'text!app/templates/items.html',
'ember'
],
/**
* View to render todos items
*
* @param String items_html, the html view for the `Todos` items
* @returns Class
*/
function( items_html ) {
return Ember.View.extend({
template: Ember.Handlebars.compile( items_html )
})
}
);
define('app/views/stats', [
'text!app/templates/stats.html',
'ember'
],
/**
* View to render todos stats
*
* @param String stats_html, stats indicator view
* @returns Class
*/
function( stats_html ) {
return Ember.View.extend({
elementId: 'todo-count',
tagName: 'span',
remainingBinding: 'controller.remaining',
template: Ember.Handlebars.compile( stats_html ),
oneLeft: function() {
return this.get( 'remaining' ) === 1;
}.observes( 'remaining' )
})
}
);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment