2014/08/13 – Backbonejs

來源:互聯網
上載者:User

標籤: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);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.