Person = Backbone.Model.extend({
initialize: function(){
alert("Welcome to Backbone!");
}
});
var person = new Person;
Person = Backbone.Model.extend({
initialize: function(){
alert("Welcome to Backbone!");
}
});
//在实例化 Model 时直接设置
var person = new Person({ name: "StephenLee", age: 22 });
//我们也可以在 Model 实例化后,通过 set() 方法进行设置
var person = new Person();
person.set({ name: "StephenLee", age: 22});
Person = Backbone.Model.extend({
initialize: function(){
alert("Welcome to Backbone!");
}
});
var person = new Person({ name: "StephenLee", age: 22});
var age = person.get("age"); // 22
var name = person.get("name"); // "StephenLee"
Person = Backbone.Model.extend({
defaults: {
name: "LebronJames",
age: 30,
},
initialize: function(){
alert("Welcome to Backbone!");
}
});
var person = new Person({ name: "StephenLee"});
var age = person.get("age"); // 因为实例化时未指定 age 值,则为默认值 30
var name = person.get("name"); //实例化制定了 name 值,则为 "StephenLee"
Person = Backbone.Model.extend({
defaults: {
name: "LebronJames",
age: 30,
hobby: "basketball"
},
initialize: function(){
alert("Welcome to Backbone!");
},
like: function( hobbyName ){
this.set({ hobby: hobbyName });
},
});
var person = new Person({ name: "StephenLee", age: 22});
person.like("coding");// 设置 StephenLee's hobby 为 coding
var hobby = person.get("hobby"); // "coding"
Person = Backbone.Model.extend({
defaults: {
name: "LebronJames",
age: 30,
},
initialize: function(){
alert("Welcome to Backbone!");
this.on("change:name", function(model){
var name = model.get("name"); // 'KobeBryant'
alert("Changed my name to " + name );
});
}
});
var person = new Person();
var age = person.set({name : "KobeBryant"});
var UserModel = Backbone.Model.extend({
urlRoot: '/user',
defaults: {
name: '',
email: ''
}
});
var UserModel = Backbone.Model.extend({
urlRoot: '/user',
defaults: {
name: '',
email: ''
}
});
var user = new Usermodel();
//注意,我们并没有在此定义 id 属性
var userDetails = {
name: 'StephenLee',
email: 'stephen.lee@mencoplatform.com'
};
//因为 model 没有 id 属性,所以此时使用 save() 方法,Backbone 会向服务器端发送一个 POST 请求,服务器端收到数据后,将其存储,并返回包含 id 的信息给 Model
user.save(userDetails, {
success: function (user) {
alert(user.toJSON());
}
})
var user = new Usermodel({id: 1});//初始化时指定 id 的值
//利用 fetch() 方法将会向 user/1 请求数据,服务器端将会返回完整的 user 记录,包含 name,email 等信息
user.fetch({
success: function (user) {
alert(user.toJSON());
}
})
var user = new Usermodel({
id: 1,
name: 'StephenLee',
email: 'stephen.lee@mencoplatform.com'
});//实例化时指定 id 值
//因为指定了 id 值,此时使用 save() 方法,Backbone 将会向 user/1 发送 PUT 请求,将会对数据库中 id 为1的记录的 email 修改
user.save({email: 'newemail@qq.com'}, {
success: function (model) {
alert(user.toJSON());
}
});
var user = new Usermodel({
id: 1,
name: 'StephenLee',
email: 'stephen.lee@mencoplatform.com'
});//实例化时指定 id 值
//因为指定了 id 值,此时使用 destroy() 方法,Backbone 将会向 user/1 发送 DELETE 请求,服务器端接收请求后,将会在数据库中删除 id 为 1的数据
user.destroy({
success: function () {
alert('Destroyed');
}
});
Model: Student, Collection: ClassStudents Model: Todo Item, Collection: Todo List Model: Animal, Collection: Zoo
Model: Student, Collection: Gym Class Model: Student, Collection: Art Class Model: Student, Collection: English Class
//定义 Model Song
var Song = Backbone.Model.extend({
defaults: {
name: "Not specified",
artist: "Not specified"
},
initialize: function(){
console.log("Music is the answer");
}
});
//定义 Collection Album
var Album = Backbone.Collection.extend({
model: Song //指定 Collection 内的 Model 为 Song
});
var song1 = new Song({ name: "How Bizarre", artist: "OMC" });
var song2 = new Song({ name: "Sexual Healing", artist: "Marvin Gaye" });
var song3 = new Song({ name: "Talk It Over In Bed", artist: "OMC" });
var myAlbum = new Album([ song1, song2, song3]);
console.log( myAlbum.models ); // 输出为 [song1, song2, song3]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有