Important things to remember:

1. Usually, we create Collection, CollectionViews, Model, View.

Collection <--> CollectionViews

Moel <--> View

2. Application can start from CollectionView or View by creating other instance.

3. Uisng a grobel App object to control everything.

4. CollectionView: the function is to render grobel interface and existing data to

the html. In initialize function, to create instance object of collection, and listen to events.

Events is model events! And don't forget calling render() fucnton.!

5. Collection just pass a model object.

6. Single modle is to fetch data and set defaults data.

7. Single view is to create tag, listenTo model events. Create user generate events{}.

(function(){
var App = {
Collections : {},
Models : {},
Views : {}
};
App.Models.ToDoItem = Backbone.Model.extend({
defaults:{firstName: "Zhentian", lastName: "Wan"}
});
App.Views.ToDoItem = Backbone.View.extend({
tagName: 'li',
initialize: function(){
_.bindAll(this, 'render', 'swap', 'remove', 'unrender');
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'remove', this.unrender);
},
events: {
'click span.swap': 'swap',
'click span.delete': 'remove'
},
render: function(){
this.$el.html('<span style="color:black;">'+this.model.get('firstName')+' '+this.model.get('lastName')+'</span> &nbsp; &nbsp; <span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;">[swap]</span> <span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;">[delete]</span>');
return this;
},
swap: function(){
var swapped = {
firstName: this.model.get('lastName'),
lastName: this.model.get('firstName')
};
this.model.set(swapped);
},
remove: function(){
this.model.destroy();
},
unrender: function(){
this.$el.remove();
}
});
App.Collections.ToDoList = Backbone.Collection.extend({model: App.Models.ToDoItem});
App.Views.ListView = new (Backbone.View.extend({
el: $('body'),
initialize: function(){
_.bindAll(this, 'render', 'appendItem', 'addItem');
this.collection = new App.Collections.ToDoList();
this.listenTo(this.collection, 'add', this.appendItem);
this.render();
this.counter = 0;
},
events:{
'click button#add': 'addItem'
},
render: function(){
this.$el.html('<button id="add">Click to add</button><ul></ul>');
return this;
},
addItem: function(){
var item = new App.Models.ToDoItem();
item.set({lastName: 'Yoona'+' '+(++this.counter)});
this.collection.add(item);
},
appendItem: function(item){
var itemView = new App.Views.ToDoItem({model: item});
$('ul', this.el).append(itemView.render().el);
}
}))();
})();
<!DOCTYPE html>
<html>
<head>
<title>Angular Directive</title>
<link rel="stylesheet" href="foundation.min.css" />
<script src="angular.min.js"></script>
<script src="main.js"></script>
</head>
<body >
<div ng-app="superApp">
<superhero flight speed strength>Superman</superhero>
<superhero speed>The Flash</superhero>
<superhero strength>The Hulk</superhero>
</div>
</body>
</html>

最新文章

  1. SQL获取日期格式
  2. docker push到本地仓库失败
  3. 关于Java项目打包
  4. leetcode 148. Sort List ----- java
  5. 桶排序-Node.js
  6. 你不需要jQuery(三):新AJAX方法fetch()
  7. Nhibernate与Dapper对比,及Nhibernate增删改和9种查询语法
  8. Winform单例模式与传值
  9. JavaScript的类型、值和变量的总结
  10. java代码中 路径符号的写法
  11. 在Github发布自己的compile包
  12. jstl c标签 ”test does not support runtime expressions“
  13. javascript 获取多种主流浏览器显示页面高度(转)
  14. html5 javascript 新增加的高级选择器更精准更实用
  15. 微服务监控zipkin+asp.net core
  16. C++学习7-面向对象编程基础(多态性与虚函数、 IO文件流操作)
  17. LabVIEW编程实例:如何通过TCP协议进行数据通信
  18. 深入理解java虚拟机读书笔记--java内存区域和管理
  19. 这个PHP无解深坑,你能解出来吗?(听说能解出来的都很秀)
  20. 荣禄[r&#243;ng l&#249;]

热门文章

  1. 希尔排序之Java实现
  2. Java 接口与抽象类
  3. 网站(Web)压测工具Webbench源码分析
  4. w​x​F​o​r​m​B​u​i​l​d​e​r​初​体​验
  5. Turtelizer 2 provide JTAG Flash programming and debugging of ARM based boards via USB
  6. mybatis配置文件,注意标签配置顺序。否则报错The content of element type &quot;configuration&quot; must match &quot;(properties?,settings?,...怎么解决
  7. linux常用命令集锦
  8. Sublime Text3 配置 Python2 Python3
  9. Ext ComboBox 动态查询
  10. 一种基于ES5的JavaScript继承