標籤:des style blog http color java os io
[來自: Backbone.js 開發秘笈 第7章]
Restful 服務調用
Collection.fetch() - 請求集合
Model.save() - 新增或修改模型(根據 Model.isNew() 方法判斷操作類型)
Model.destroy() - 刪除模型
Model.sync() - 請求的執行方法, fetch(), save(), destroy() 都調用其方法進行操作請求
(function ($) { //define ------------------------------- var Product = Backbone.Model.extend({ idAttribute: "Id", url: "/ProductService.svc/Product"//Rest URL }); var ProductCollection = Backbone.Collection.extend({ model: Product, url: "/ProductService.svc/products"//rest URL }); var ProductListView = Backbone.View.extend({ tagName: "ul", events: { "click": "selectName" }, selectName: function (e) { if (e.target.nodeName === "A") { this.collection.trigger("selected", this.collection.get($(e.target).attr("data-id"))); } }, render: function () { this.$el.html(_.map(this.collection.models, function (model) { return "<li><a href=‘javascript:;‘ data-id=‘" + model.get(‘Id‘) + "‘>" + model.get(‘Name‘) + "</a></li>"; })); return this; } }); var EditPanelView = Backbone.View.extend({ tagName: "div", events: { "click .btnUpdate": "update", "click .btnCreate": "create", "click .btnDestroy": "destroy" }, update: function () { /* Backbone MethodMap create: "POST" delete: "DELETE" patch: "PATCH" read: "GET" update: "PUT" */ /* 參數二 { patch:true // is PATCH method wait:true //同步方法 } */ this.model.save({ ‘Name‘: this.$el.find("input[type=‘textBox‘]")[0].value }, { success: this.success, operType: "update" }); }, create: function () { //Collection.create() 方法回調用 Model.save() 方法 new Product({ Name: this.$el.find("input[type=‘textBox‘]")[0].value }).save(null, { success: this.success, operType: "create" }); }, destroy: function () { this.model.destroy({ success: this.success, url: this.model.url + "/" + this.model.get(‘Id‘), operType: "destroy" }); }, success: function (model, data, options) { //operate callback switch (options.operType) {//operType 自訂屬性,標識回呼函數的操作類型 case "create": break; case "update": break; case "destroy": break; default: } }, render: function () { this.$el.html("<label>Name:</label><input type=‘textBox‘ value=‘" + this.model.get("Name") + "‘>" + "<input type=‘button‘ class=‘btnUpdate‘ value=‘Update‘/>" + "<input type=‘button‘ class=‘btnCreate‘ value=‘Create‘/>" + "<input type=‘button‘ class=‘btnDestroy‘ value=‘Destroy‘/>"); return this; } }); $(function () { //instance --------------------------------- var productCollection = new ProductCollection(); //fetch() 方法擷取資料 [success and error 是成功的和異常的回呼函數] /* fetch, save, destroy 方法都會調用 sync() 方法來執行 HTTP 要求 */ /* sync() 方法參數 method - [create, update, patch, delete, read] model - 需要同步的模型或集合 options - $.ajax 所需要的參數 */ productCollection.fetch({ success: function (collection, response, options) { collection.on("selected", function (model) { $("#divEdit").html(new EditPanelView({ model: model }).render().el); }); $(‘body‘).html(new ProductListView({ collection: collection }).render().el); $(‘body‘).append($("<div id=‘divEdit‘></div>")); }, error: function (collection, response, options) { alert(‘error‘); } }); });})(jQuery);