【问题标题】:Backbone collection's URL depends on initialize functionBackbone 集合的 URL 取决于初始化函数
【发布时间】:2012-04-03 23:19:40
【问题描述】:

我有一个 Backbone 集合,其 URL 取决于初始化函数。当我创建这个 Backbone 集合的一个实例时,我传入一个 ID 来过滤模型的哪些实例出现。下面是集合的代码:

  var GoalUpdateList = Backbone.Collection.extend({

    // Reference the Goal Update model
    model: GoalUpdate,

    // Do HTTP requests on this endpoint
    url: "http://localhost:8000/api/v1/goal_update/?goal__id=" + this.goal_id + "&format=json",

    // Set the goal ID that the goal update list corresponds to
    initialize: function(goal_id) {
      this.goal_id = goal_id;
      console.log(this.goal_id);
      console.log(this.url);
    },

  });

当然,这是行不通的。 this.goal_id 被视为未定义。我猜是因为 URL 是在初始化函数运行之前设置的。

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    您可以为url 使用动态构建URL 的函数。

    url: function() {
      return "http://localhost:8000/api/v1/goal_update/?goal__id=" + this.goal_id + "&format=json";
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多