Backbone 学习笔记四

has    model.has(attribute)

属性值为非  null 或undefined ,有则返回true

if(note.has("title")) {

}

unset  model.unset(attribute,[options])

从内部属性中删除指定属性,会触发change事件

clear  清除所有属性值

model.clear();

id   model.id

模型的特殊属性,可以是任意字符串。在属性中设置的id会直接拷到模型上。我们可以从集合中通过id获取模型,另外id可以生成模型的URLs.

cid  mode.cid

模型的特殊属性,   模型创建时自动生成的唯一标识符。客户ids在未保存到服务器之前前便宜存在,可能不具有最终的ids.

attributes   model.attributes

模型内部散列表。建议采用set更新属性而不要直接的修改。如果获取模型属性的副本,用toJSON取而代之。

defaults

用于模型指定默认属性。创建模型实例时,任何未指定的属性会被告设置为其默认值

var Medal = Backbone.Model.extend({

  defaults:{

    "entree": "ravioli"

  }

});

alert("Dessert will be " + (new Meal).get('dessert'));

toJSON   返回 模型attributes 副本的JSON字符串形式,用于模型的持久化、序列化,或者传递到视力前的扩充。

var artist = new Backbone.Model({

  firstName:"立华",

  lastName:"咸"

});

alert(artist.toJSON);

fetch   model.fetch

从服务器重置模型状态。这对模型尚未填充数据,或者服务器已有最新状态的情况很有用处。,如果服务器端状态与前属性不同,刚触发hcange事件,

选项表的散列表参数接受success和error回调函数,回调函数可以接受者model ,response作为为参数

setInterval(function(){

  channel.fetch();

},10000);

save  通过委托Backbone.sync保存模型到数据库(或可替代的持久层)

模型isNew     采用create方法,如果模型已经在服务器存在,保存将采用update方法

Backbone.sync = function(mehtod,model) {

  alert(method + ":"  + JSON.stringify(model));

     model.id = 1;

}

var book = new Backbone.Model({

  title:"The Rough Riders",

  author:"The odore Roosevelt"

});

book.save();

book.save({author:"Teddy"});

可以传入 model response 作为参数,  如果模型拥有validate 方法并且验证失败,error回调函数会执行,如果服务器难失败,返回非200 http状态码

将产生文本或JSON的错误内容简介

book.save({author:"F.D.R"},{error:function(){

}});

原文地址:https://www.cnblogs.com/yushunwu/p/2293682.html