使用require.js和backbone實現簡單單頁應用實踐

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   使用   sp   java   

前言

最近的任務是重做公司的觸屏版,於是再園子裡各種逛,想找個合適的架構做成Web App.看到了葉大(http://www.cnblogs.com/yexiaochai/)對backbone的描述和他做的一個簡單架構demo(http://www.cnblogs.com/yexiaochai/p/3837713.html),於是拿來主義,把源碼下載了.

項目架構

項目使用VS2012+MYSQL開發,MVC架構,沒有葉大那麼厲害,只能做做簡單應用,下面的做的一個列表和詳細頁面的JS代碼塊.

主要JS代碼塊:

<script type="text/javascript">        var IndexModel = Backbone.Model.extend({});    var IndexList = Backbone.Collection.extend({        model: IndexModel,        parse: function (data) {            return (data && data.data) || {}        },        setComparator: function (type) {            this.comparator = function (item) {                return Math.max(item.attributes[type]);            }        }    });    var Detail = Backbone.View.extend({        el: $("#contents"),        template: _.template($(‘#detail-template‘).html()),        events: {            ‘click #js-return‘: function () {                this.app.forward(‘index‘);            }        },        initialize: function (app) {            this.app = app;            this.render();        },        render: function () {            var scope = this;            var id = this.app.id;                       var model = this.app.model;            $.ajax({                url: ‘@Url.Action("GetContentById", "Home")‘,                type: ‘get‘,                data: { id: id },                dataType: ‘json‘,                timeout: 1000,                error: function () { location.href = ‘/‘; },                success: function (data) {                    if (data && data.data) {                        model.set(‘value‘, data.data);                        $(".viewport").hide();                        $(‘#viewport_detail‘).show();                                $(‘#id_viewport_detail‘).html(scope.template(model.toJSON()));                    }                }            });        }    });    var Index = Backbone.View.extend({            el: $(‘#indexlist‘),            template: _.template($(‘#indexlist-template‘).html()),            events: {                ‘click .js_largeimg‘: function (e) {                    var el = $(e.currentTarget);                    var index = el.attr(‘data-index‘);                    var id = el.attr(‘data-id‘);                    var model = this.list.models[index];                    this.app.model = model;                    this.app.id = id;                    this.app.forward(‘detail/‘+id);                }            },            initialize: function (app) {                this.app = app;                var scope = this;                var curpage = 1;                var pageSize = 10;                this.list = new IndexList();                this.list.url = ‘@Url.Action("GetIndexList", "Home")‘;                this.list.fetch({                    success: function () {                        scope.render();                    }                });                this.listenTo(this.list, ‘all‘, this.render);            },            render: function () {                var models = this.list.models;                var html = ‘‘;                for (var i = 0, len = models.length; i < len; i++) {                    models[i].index = i;                    html += this.template(_.extend(models[i].toJSON(), { index: i }));                }                $(".viewport").hide();                $("#viewport_index").show();                $("#indexlist").html(html);                var s = ‘‘;            }        });        var App = Backbone.Router.extend({            routes: {                "": "index",    // #index                "index": "index",    // #index                "detail/:id": "detail"   // #detail            },            index: function () {                var index = new Index(this.interface);            },            detail: function (id) {                var detail = new Detail(this.interface);                detail.app.id = id;            },            initialize: function () {            },            interface: {                forward: function (url) {                                        window.location.href = (‘#‘ + url).replace(/^#+/, ‘#‘);                }            }        });        var app = new App();        Backbone.history.start();        var s = ‘‘;</script>  

實現效果不錯,繼續努力不斷最佳化ing..........

使用require.js和backbone實現簡單單頁應用實踐

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.