简体   繁体   中英

Requirejs not passing backbone model id

Since I have switched to require.js for my app using backbone it appears that my model is no longer getting or adding my ID to the URL like it use to.

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

when I console out the model the url is /api/campaign/ when before it was /api/campaign/1/

What I'm I doing wrong?

View:

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;

});

Model:

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

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

  return CampaginModel;

});

This is not a require.js issue. You are defining urlRoot and therefore "overwriting" the default URL in the form "[collection.url]/[id]", so you have to add the idAttribute to your definition:

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

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM