构建我的backbone.js应用程序

LDK*_*LDK 3 requirejs backbone.js

我正在使用Require.js创建Backbone.js应用程序.每个视图文件对应一个资源(例如"新闻").在每个视图文件中,我为每个动作('index','new'等)声明了一个骨干视图.在视图文件的底部,我从路由器接收必要的信息,然后决定实例化哪个视图(基于从路由器传入的信息).

这一切都运行良好,但它需要大量代码,似乎不是'backbone.js方式'.首先,我依靠网址管理状态.另一方面,我没有使用在很多backbone.js例子中弹出的_.bind.换句话说,我不认为我做得对,我的代码库闻起来......有关如何更好地构建我的应用程序的任何想法?

router.js

define([
  'jquery',
  'underscore',
  'backbone',
  'views/news'], 
  function($, _, Backbone,  newsView){
    var AppRouter = Backbone.Router.extend({
        routes:{
            'news':'news',
            'news/:action':'news',
            'news/:action/:id':'news'
        },

        news: function(action, id){
            newsView(this, action, id).render();
        }
    });


    var intialize = function(){
        new AppRouter;
        Backbone.history.start()
    };

    return{
        initialize: initialize;
    };
}
Run Code Online (Sandbox Code Playgroud)

news.js('views/news')

define([
  'jquery',
  'underscore',
  'backbone',
  'collections/news',
  'text!templates/news/index.html',
  'text!templates/news/form.html'

  ], function($, _, Backbone, newsCollection, newsIndexTemplate, newsFormTemplate){

    var indexNewsView = Backbone.View.extend({
        el: $("#content"),

        initialize: function(router){
            ...
        },

        render: function(){
            ...
        }
    });

    var newNewsView = Backbone.View.extend({
        el: $("#modal"),

        render: function(){
            ...
        }
    });

    ...

    /*
     *  SUB ROUTER ACTIONS
     */

    var defaultAction = function(router){
      return new newsIndexView(router);
    }

    var subRouter = {
      undefined: function(router){return defaultAction(router);},

      'index': function(router){ return defaultAction(router);},

      'new': function(){
        return new newNewsView()
      },

      'create': function(router){
        unsavedModel = {
          title : $(".modal-body form input[name=title]").val(),
          body  : $(".modal-body form textarea").val()
        };
        return new createNewsView(router, unsavedModel);
      },

      'edit': function(router, id){
        return new editNewsView(router, id);
      },

      'update': function(router, id){
        unsavedModel = {
          title : $(".modal-body form input[name=title]").val(),
          body  : $(".modal-body form textarea").val()
        };

        return new updateNewsView(router, id, unsavedModel);
      },
    }

    return function(router, action, id){
      var re = /^(index)$|^(edit)$|^(update)$|^(new)$|^(create)$/
      if(action != undefined && !re.test(action)){
        router.navigate('/news',true);
      }
      return subRouter[action](router, id);
    }


  });
Run Code Online (Sandbox Code Playgroud)

nra*_*itz 6

虽然我觉得重要的是强调没有真正的"Backbone.js方式",但看起来你似乎正在复制Backbone应该为你做的工作.

我同意为应用程序的每个独立部分配备专用路由器是有意义的.但它首先看一下你在"子路由器"部分中所做的只是重新创建Backbone.Router功能.您AppRouter根本不需要处理/newsURL; 您可以NewsRouter使用特定于新闻的路线初始化a ,它将处理与新闻相关的URL:

var NewsRouter = Backbone.Router.extend({
    routes:{
        'news':             'index',
        'news/create':      'create',
        'news/update/:id':  'update',
        'news/edit/:id':    'edit'
    },

    index: function() { ... },

    create: function() { ... },

    // etc
});
Run Code Online (Sandbox Code Playgroud)

只要在您调用之前初始化Backbone.history.start()它,它将捕获其路由的URL请求,您永远不必处理AppRouter.您也不需要处理视图底部的丑陋代码 - 这基本上只是在做核心Backbone.Router为您做的事情.