Skip to content

Commit b421885

Browse files
committed
添加了 backbonejs.org,学习了 backbone.js 基础
1 parent f7667c3 commit b421885

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed

backbonejs.org

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
* Model
2+
** 综述
3+
Backbone models contain interactive data for an application as well as the
4+
logic around this data.
5+
6+
例:
7+
8+
var Photo = Backbone.Model.extend({
9+
defaults: {
10+
src: 'placeholder.jpg',
11+
title: 'an image placeholder',
12+
coordinate: [0, 0],
13+
},
14+
15+
initialize: function() {
16+
this.on("change:src", function() {
17+
var src = this.get("src");
18+
console.log("Image source updated to " + src);
19+
});
20+
},
21+
22+
changeSrc: function(source) {
23+
this.set(
24+
src: source,
25+
);
26+
},
27+
});
28+
** obj.get()
29+
*obj.get()* provides easy access to a model's attributes.
30+
** obj.set()
31+
设置属性.
32+
** obj.attributes (不建议使用)
33+
可通过 Model 对象实例的 *attributes* 属性获得该对象的所有属性.
34+
** obj.toJSON()
35+
使用方法:
36+
37+
var myAttributes = Model对象.toJSON();
38+
39+
返回 json 格式的 Model对象 的全部属性.
40+
** obj.defaults 属性
41+
可在初始化 Model 时设置默认该 Model 默认的属性值.
42+
43+
类似于 Python class 的 __init__() 中设置默认的值.
44+
** obj.initialize 属性 (值是函数)
45+
类似于 Python class 的 __init__() 作用.
46+
** listening for changes to your model
47+
Any and all of the attributes in a Backbone model can have listeners bound
48+
to them which detect when their values change. Listeners can be added to the
49+
initialize() function.
50+
** obj.validate 属性 (值是函数)
51+
It allows checking the attribute values for a model prior to them being set.
52+
If the attributes provided are valid, nothing should be returned
53+
from *.validate()* . If they are invalid, a custom error can be returned
54+
instead.
55+
56+
例:
57+
58+
var Photo = Backbone.Model.extend({
59+
validate: function(attribs) {
60+
if (attribs.src === undefined) {
61+
return "Remember to set a source for your image!";
62+
}
63+
},
64+
65+
initialize: function() {
66+
console.log("this model has been initialized");
67+
this.on("error", function(model, error) {
68+
console.log(error);
69+
});
70+
},
71+
});
72+
73+
var myPhoto = new Photo();
74+
myPhoto.set({
75+
title: "On the beach",
76+
});
77+
// 会报出这样错误: 'Rember to set a source for your image!'
78+
* View
79+
** 综述
80+
Views in Backbone don't contain the markup for your application, but rather
81+
they are to support models by defining the logic for how they should be
82+
represented to the user. This is usually achieved using JavaScript
83+
templating (e.g. Mustache, jQuerytmpl, etc.). A view's *render()* function
84+
can be bound to a model's *change()* event, allowing the view to always be
85+
up to date without requiring a full page refresh.
86+
87+
例:
88+
89+
var PhotoSearch = Backbone.View.extend({
90+
el: $("#results");
91+
92+
render: function(event) {
93+
var compiled_template = _.template($("#results-template").html());
94+
this.$el.html(compiled_template(this.model.toJSON()));
95+
96+
// recommended as this enables calls to be chained
97+
return this;
98+
},
99+
100+
events: {
101+
"submit #searchForm": "search",
102+
"click .reset": "reset",
103+
"click .advanced": "switchContext",
104+
},
105+
106+
search: function(event) {
107+
// executed when a form '#searchForm' has been submitted
108+
},
109+
110+
reset: function(event) {
111+
// executed when an element with class "reset" has been clicked
112+
},
113+
114+
switchContext: function(event) {
115+
// executed when an element with class "advanced" has been clicked
116+
},
117+
});
118+
** obj.el
119+
*el* is basically a reference to a DOM element and all views must have
120+
one. It allows for all of the contents of a veiw to be inserted into the DOM
121+
at once, which makes for faster rendering as browser performs the minimum
122+
required reflows and repaints.
123+
124+
There are two ways to attach a DOM element to a view: the element already
125+
exists in the page or a new element is created for the view and added
126+
manually by the developer. If the element already exists in the page, you
127+
can set *el* as either a CSS selector that matches the element or a simple
128+
reference to the DOM element.
129+
** obj.render (值是函数)
130+
*render()* is an optional function that defines the logic for rendering a
131+
template.
132+
133+
The *.template()* method in Underscore compiles JavaScript templates into
134+
functions which can be evaluated for rendering.
135+
** obj.events
136+
The Backbone *events* attribute allows us to attach event listeners to
137+
either custom selectors, or directly to *el* if no selector is provided. An
138+
event takes the form
139+
*{"eventName selector": "callbackFunction"}*
140+
and a number of event-types are supported.
141+
142+
The only thing to really keep in mind is that any string callback supplied
143+
to the events attribute must have a corresponding function with the same
144+
name within the scope of your view.
145+
146+
* Collections
147+
** 综述
148+
Collections are sets of Models and are created by
149+
extending *Backbone.Collection*.
150+
151+
Normally, when creating a collection you'll also want to pass through a
152+
property specifying the model that your collection will contain, as well as
153+
any instance properties required.
154+
** Getters and Setters
155+
*** obj.get(id)
156+
e.g.
157+
var skiingEpicness = PhotoCllection.get(2);
158+
*** obj.getByCid(cid)
159+
The client id is a property that Backbone automatically assigns models that
160+
have not yet been saved.
161+
You can get a model's client id from its *.cid* property.
162+
163+
e.g.
164+
var mySkiingCrash = PhotoCollection.getByCid(456);
165+
*** obj.add() and obj.remove()
166+
e.g.
167+
var a = new Backbone.Model({title: 'my vacation'}),
168+
b = new Backbone.Model({title: 'my holiday'});
169+
170+
var photoCollection = new PhotoCollection([a, b]);
171+
photoCollection.remove([a, b]);
172+
*** listening for events
173+
As collections represent a group of items, we're also able to listen
174+
for *add* and *remove* events for when new models are added or removed from
175+
the collection.
176+
*** fetching models from the server
177+
*Collections.fetch()* retrieves a default set of models from the server in
178+
the form of a JSON array. When this data returns, the current collection's
179+
contents will be replaced with the contents of the array.
180+
*** resetting/refreshing collections
181+
Rather than adding or removing models individually, you might occasionally
182+
wish to update an entire collection at once.
183+
*Collection.reset()* allows us to replace an entire collection with new
184+
models.
185+
* Underscore utility functions
186+
** 综述
187+
Backbone 依赖于 Underscore.js,所以可以直接使用 Underscore.js 的方法.
188+
* Routers
189+
** 综述
190+
In Backbone, routers are used to help manage application state and for
191+
connecting URLs to application events. This is achieved using hash-tags with
192+
URL fragments, or using the browser's pushState and History API.
193+
e.g.
194+
http://unicorns.com/#whatsup
195+
http://unicorns.com/#search/seaonal-horns/page2
196+
197+
Note: An application will usually have at least one route mapping a URL
198+
route to a function that determines what happens when a user reaches that
199+
particular route.
200+
* Backbone.history (不太理解作用)
201+
It handles *hashchange* events in our application. This will automatically
202+
handle routes that have been defined and trigger callbacks when they've been
203+
accessed.
204+
205+
The *Backbone.history.start()* method will simply tell Backbone that it's OK
206+
to begin monitoring all *hashchange* events.
207+
208+
As an aside, if you would like to save application state to the URL at a
209+
particular point, you can use the *.navigate()* method to achieve this. It
210+
simply updates your URL fragment without the need to trigger the *hashchange*
211+
event.
212+
213+
It is also possible for *Router.navigate()* to trigger the route as well as
214+
updating the URL fragment.
215+
* Namespacing
216+
** 避免命名冲突的几种方式
217+
*** 变量添加前缀
218+
e.g.
219+
220+
var db_host,
221+
db_user,
222+
db_passwd;
223+
*** 在对象中命名变量
224+
e.g.
225+
226+
var my_profile = my_profile || {};
227+
my_profile.name = 'flyer';
228+
my_profile.sex = 'male';
229+
*** nested namespacing
230+
注意其中的大小写.
231+
e.g.
232+
233+
var galleryApp = galleryApp || {};
234+
235+
galleryApp.routers = galleryApp.routers || {};
236+
galleryApp.model = galleryApp.model || {};
237+
galleryApp.model.special = galleryApp.model.special || {};
238+
239+
// routers
240+
galleryApp.routers.Workspace = Backbone.Router.extend({});
241+
galleryApp.routers.PhotoSearch = Backbone.Router.extend({});
242+
243+
// models
244+
galleryApp.model.Photo = Backbone.Model.extend({});
245+
galleryApp.model.Comment = Backbone.Model.extend({});
246+
247+
// special models
248+
galleryApp.model.spider.Admin = Backbone.Model.extend({});

linux_tips.org

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,15 @@
10001000

10011001
然后就可以通过方向键上下移动了.
10021002
退出这种模式按 "q"
1003+
** 复制粘贴
1004+
先按
1005+
1006+
命令前缀 [
1007+
1008+
然后根据设置的 emacs 或 vim 模块进行复制的操作.
1009+
退出该模式,按下如下键执行粘贴操作:
1010+
1011+
命令前缀 ]
10031012
* 在远程机器中上传和下载文件
10041013
一般可通过 scp 来实现,但在公司中,可能不能使用这条命令。
10051014
Windows 下可通过 secureCRT 的 rz 和 sz 命令实现文件的上传和下载功能。

0 commit comments

Comments
 (0)