使用require.js和backbone实现简单单页应用实践

前言

最近的任务是重做公司的触屏版,于是再园子里各种逛,想找个合适的框架做成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..........

时间: 2024-08-30 15:10:42

使用require.js和backbone实现简单单页应用实践的相关文章

html5手机Web单页应用实践--起点移动阅读

一开始以hybrid形式做了一个android的小说阅读客户端,叫4G阅读.而后由于业务需求,要迅速实现纯手机html5 版的,所以就直接在原先客户端内内嵌的网页进行改版,快速实现以后在优化的过程中发现越改越多越改越多- 注意此web应用只支持android及iphone内的浏览器,及PC或mac上的chrome,safari,firefox等支持html5的浏览器.IE10以上浏览器 这算是试验版了吧,以前没这么弄过.. 手机访问http://crapi.4gshu.com:8096/4g-r

【笔记】关于require.js 的用法

最近忙于学校的一个新网站建设,对于以前的前端程序编写方式的不正规特意上网学习了require.js 的用法,使此次的工程更加有条理同时符合当前前端的开发模式--前端模块化. 网上有不少很好的学习文章这里推荐阮一峰老师的:http://www.ruanyifeng.com/blog/2012/11/require_js.html 下面是本人归纳的一些要点: 1.使用require.js 必须从官网下载 require.js 文件 http://www.requirejs.cn/ 打开官网便可以下载

单页应用和多页应用

多页面应用 一个项目是由多个完整的html页面组成, 每一次页面跳转的时候,后台服务器都会给返回一个新的HTML文档,页面跳转所有的资源都要重新加载,页面之间的切换会出现卡顿空白的问题,不容易实现切换动画等.这种类型的网站也就是多页网站,也叫做多页应用. 为什么多页应用的首屏时间快? 首屏时间叫做页面首个屏幕的内容展现的时间,当我们访问页面的时候,服务器返回一个html,页面就会展示出来,这个过程只经历了一个HTTP请求,所以页面展示的速度非常快. 为什么搜索引擎优化效果好(SEO)? 搜索引擎

单页应用及多页应用

一:是什么 多页面应用:一个项目是由多个完整的html页面组成 单页面应用:一个项目中只有一个完整的html页面,其他的都是部分html片段组成. 二:渲染方式 多页面:页面跳转,后台服务器返回一个新的html文档,页面跳转所有资源都要重新加载 单页面:页面跳转,局部刷新,不会重新加载全部资源.片段切换快,容易实现 二:问题: 多页面:页面跳转切换,会出现卡顿空白问题,不容易实现切换动画.切换慢 单页面:首屏时间慢,SEO差:单页应用的首屏时间慢,首屏时需要请求一次html,同时还要发送一次js

require.js+backbone 使用r.js 来压缩,本地不压缩,生产环境压缩 的实现方式

requie.js 和backbone.js 这里就不说了,可以去看官方文档,都很详细! 但是使用require.js 默认带的压缩方式感觉不是很方便,所以本文主要讲 利用r.js压缩,来实现本地不压缩,生产环境压缩 r.js 是运行在node上的,默认使用UglifyJS.UglifyJS真的很好用,那为什么说默认的方式 不是很方便呢? r.js 单独压缩一个文件也很好用的,但在实际项目中,总不能一个一个压吧!因此r.js提供了一种多文件的压缩方式 ,使用一个叫bulid.js 的配置文件来配

require.js+backbone 使用r.js 在本地与生产环境 一键压缩的实现方式

requie.js 和backbone.js 这里就不说了,能够去看官方文档,都非常具体! 可是使用require.js 默认带的压缩方式感觉不是非常方便,所以本文主要讲 利用r.js压缩,来实现本地不压缩,生产环境压缩 r.js 是执行在node上的,默认使用UglifyJS.UglifyJS真的非常好用,那为什么说默认的方式 不是非常方便呢? r.js 单独压缩一个文件也非常好用的,但在实际项目中.总不能一个一个压吧!因此r.js提供了一种多文件的压缩方式 ,使用一个叫bulid.js 的配

require.js的简单使用

<script src="js/require.js"></script> <script src="js/require.js" data-main="js/main"></script> require(['jquery', 'underscore', 'backbone'], function ($, _, Backbone){ // some code here }); 使用require.

undercore &amp; Backbone对AMD的支持(Require.js中如何使用undercore &amp; Backbone)

RequireJS填补了前端模块化开发的空缺,RequireJS遵循AMD(异步模块定义,Asynchronous Module Definition)规范,越来越多的框架支持AMD,像最近的jQuery,但有一些也不支持,像Backbone,那如何在RequireJS使用Backbone呢?需要使用RequireJS设置它们的一些特性:如下 require.config({ paths:{ jquery : 'jquery-1.7.2', underscore : 'underscore',

require.js 的简单运用 --兰

功能描述: 1.简单的require.js的运用 2.BaseUrl运用(加载不同文件夹下的路径) 3.shim运用(依赖:比如jquery-ui 需要依赖jquery) 4.require方法调用(包括回调方法使用) 1.文件的目录结构 main.js代码 require.config({ baseUrl:"js", paths: { jquery: 'jquery-1.7.2', bootstrap_alert:'bootstrap-alert', model1:'model1/m