将变量从route传递到view/collection/model


Backbone.js - Pass variable from route to view/collection/model

所以我正在建立一个移动网站,我有一个名为'api'的目录,其中包含从远程api返回JSON格式数据的各种php文件。我这样做是为了避免跨域问题。

但是其中一个php文件需要一个GET参数(即id),这样我就可以根据它的id回显特定对象的JSON数据。

我的集合将需要这样做(假设这将工作):

define([
    'backbone',
    'models/tournaments/single'
], function(Backbone, singleModel) {
    var TournamentCollection = Backbone.Collection.extend({
        model: singleModel,
        url: '/api/tournament.php?id=' + id,
        parse: function(response) {
            return response;
        }
    });
    return TournamentCollection;
});

我有这个在我的路由器,但我如何将'id'值传递给视图或集合:

define([
    'jquery',
    'underscore',
    'backbone',
    'views/home',
    'views/tournament'
], function($, _, Backbone, HomeView, TournamentView) {
    var AppRouter = Backbone.Router.extend({
        routes: {
            '': 'home',
            'tournament/:id': 'tournament'
        }
    });
    var initialize = function() {
        var app_router = new AppRouter;
        app_router.on('route:home', function() {
            var homeView = new HomeView();
        });
        app_router.on('route:tournament', function(id) {
            var tournamentView = new TournamentView({id: id});
        });
        Backbone.history.start();
    };
    return {
        initialize: initialize
    };
});

几点:

1)你对集合的url属性的定义将无法工作,因为在定义锦标赛集合类时可能没有定义id。您可以使用函数而不是属性。TournamentCollection会变成这样:

define([
    'backbone',
    'models/tournaments/single'
], function(Backbone, singleModel) {
    var TournamentCollection = Backbone.Collection.extend({
        model: singleModel,
        initialize: function (options) {
          this.id = options.id;
        },
        url: function () {
          return '/api/tournament.php?id=' + this.id
        },
        parse: function(response) {
            return response;
        }
    });
    return TournamentCollection;
});

这样你就可以用一个id初始化对象,然后,当url被获取时,它将包含正确的id。

2)我可能会初始化并从路由器获取集合。然后从视图的初始化开始,等待取回完成并最终重新呈现视图。像这样:

define([
    'jquery',
    'underscore',
     'backbone',
     'views/home',
     'views/tournament'
 ], function($, _, Backbone, HomeView, TournamentView) {
     var AppRouter = Backbone.Router.extend({
         routes: {
             '': 'home',
             'tournament/:id': 'tournament'
         }
     });
     var initialize = function() {
         var app_router = new AppRouter;
         app_router.on('route:home', function() {
             var homeView = new HomeView();
         });
         app_router.on('route:tournament', function(id) {
             var tournaments = new TournamentCollection({ id: id });
             tournaments.fetch();
             var tournamentView = new TournamentView({ collection: tournaments });
         });
         Backbone.history.start();
     };
     return {
         initialize: initialize
     };
 });
 // Tournament View define stuff
 var TournamentView = Backbone.View.extend({
   initialize: function () {
     this.listenTo(this.collection, 'sync', this.render);
   },
   render: function () {
     //...
   }
 });
 return TournamentView

希望有帮助。:)