0

由于我已使用主干为我的应用程序切换到 require.js,因此我的模型似乎不再像以前那样获取或添加我的 ID 到 URL。

  var campaign = new CampaginModel({id: options.id});

当我安慰模型/api/campaign/时,网址是它之前的时间/api/campaign/1/

我做错了什么?

看法:

define([
  'jquery',
  'underscore',
  'backbone',
  'views/RewardView',
  'views/FriendRewardView',
  'models/CampaginModel',
  'text!templates/backbone/portal/campaignTemplate.html'
], function($, _, Backbone, rewardView, friendRewardView, CampaginModel, campaignTemplate){



   var CampaginView = Backbone.View.extend({
       el: '#campaign-panel',

        // View constructor
        initialize: function() {
            // Storing the View context
            self = this;
        },


        render: function(options) {
            if(options.id){


                var campaign = new CampaginModel({id: options.id});

                console.log('model', campaign)

                campaign.fetch({
                    success: function(campaign){
                        // We can only get the reward when the campaign reward url is returned.

                        var rewardview = new RewardView();
                        rewardview.render({reward_url: campaign.get('participant_reward')});


                        var friendview = new FriendRewardView();
                        friendview.render({reward_url: campaign.get('friend_reward')});


                        var template = _.template(campaignTemplate, {campaign: campaign});
                        this.$el.html(template);


                    }// end success

                    ,

                    error: function (model, response) {
                      console.log('error', model, response);
                    }// end of error

                }); // end fetch
            }// end if option.id
        } // end render function
    }); // end campagin view


  return CampaginView;

});

模型:

define([
  'underscore',
  'backbone'
], function(_, Backbone) {

    var CampaginModel = Backbone.Model.extend({
       urlRoot: '/api/v1/campaign/'
    });

  return CampaginModel;

});
4

1 回答 1

1

这不是 require.js 问题。您正在定义 urlRoot 并因此以“[collection.url]/[id]”的形式“覆盖”默认URL,因此您必须将 idAttribute 添加到您的定义中:

var CampaginModel = Backbone.Model.extend({
   urlRoot: '/api/v1/campaign/',
   idAtttribute: 'id'
});

干杯。

于 2013-11-11T12:01:20.270 回答