A simplier example with less code

This example is a little simplier and probably faster because it:
 * does not create a new closure
 * does not call a closure
 * does not create a new variable (vm)
 * does not search for that variable in two scopes every time it needs to be used
This commit is contained in:
Daniel Santana 2015-03-23 17:05:17 -04:00
parent b0f85a7131
commit e37c425d82

View file

@ -151,6 +151,29 @@ todo.vm = (function() {
}())
```
or
```javascript
//define the view-model
todo.vm = {
init: function() {
//a running list of todos
this.list = new todo.TodoList();
//a slot to store the name of a new todo before it is created
this.description = m.prop('');
//adds a todo to the list, and clears the description field for user convenience
this.add = function(description) {
if (description()) {
this.list.push(new todo.Todo({description: description()}));
this.description("");
}
};
}
};
```
The code above defines a view-model object called `vm`. It is simply a javascript object that has a `init` function. This function initializes the `vm` object with three members: `list`, which is simply an array, `description`, which is an `m.prop` getter-setter function with an empty string as the initial value, and `add`, which is a method that adds a new Todo instance to `list` if an input description getter-setter is not an empty string.
Later in this guide, we'll pass the `description` property as the parameter to this function. When we get there, I'll explain why we're passing description as an argument instead of simply using OOP-style member association.