Backbone.js - 将变量从路径传递到视图/集合/模型

时间:2014-08-14 20:46:34

标签: javascript php jquery json backbone.js

所以我建立了一个移动网站,我有一个叫做“api”的目录。与各种php文件帽子从远程API回应JSON格式的数据。我这样做是为了避免跨域问题。

但其中一个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 个答案:

答案 0 :(得分:1)

一些事情:

1)您对集合的url属性的定义将不起作用,因为在定义TournamentCollection类时可能未定义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)我可能会初始化并从路由器中获取集合。然后从视图的初始化开始,监听该fetch以完成并最终重新渲染视图。像这样:

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
希望有所帮助。 :)

相关问题