Observables

Knockout is built around three core features:

  1. Observables and dependency tracking
  2. Declarative bindings
  3. Templating

 

Model-View-View Model (MVVM) is a design pattern for building user interfaces. It describes how you can keep a potentially sophisticated UI simple by splitting it into three parts:

  • model: your application’s stored data. This data represents objects and operations in your business domain (e.g., bank accounts that can perform money transfers) and is independent of any UI. 

  • view model: a pure-code representation of the data and operations on a UI.

  • view: a visible, interactive UI representing the state of the view model.

 

<script type='text/javascript' src='knockout-3.2.0.js'></script>

My name is <span data-bind="text: name"></span>,
<span data-bind="text: age"></span> years old

<script>
    //ViewModel
    var myViewModel={
        name:ko.observable('allen'),
        age:ko.observable(25)
    };
    //writing observable
    myViewModel.name('Mary').age(50);
    //applyBindings
    ko.applyBindings(myViewModel);
    //event
    myViewModel.name.subscribe(function(newValue) {
        alert("The person's new name is " + newValue);
    });    
    //writing observable
    myViewModel.name('alisa').age(25);
</script>

 

 

http://blog.csdn.net/eqera/article/details/8437349

  

posted @ 2014-11-26 16:53  小-黑  阅读(123)  评论(0编辑  收藏  举报