0

我正在使用 require.js 和主干。我的问题:我如何从我的视图中获取。()我的模型。我尝试过的内容如下,但是我收到错误“广告系列未定义”。我想我非常接近:

模型:

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

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

  return Campagin;

});

看法:

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


   var CampaginView = Backbone.View.extend({
       el: '#campaign-panel',
        render: function(options) {
            if(options.id){

                var campaign = new Campagin({id: options.id});
                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
                }); // end fetch
            }// end if option.id
        } // end render function
    }); // end campagin view


  return CampaginView;

});
4

1 回答 1

1

在您的视图中,您正在指定一个依赖项数组,这些依赖项将作为函数参数传递给定义函数,并以与数组中的顺序相同的顺序列出。但是您只声明了 4 个参数:$(jQuery)、_(下划线)BackbonecampaignTemplate(这是错误的,因为根据您的依赖项应该是RewardView)。所以你必须正确地声明你的函数。例如:

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

   ...
}

查看 Require JS 的文档以获取更多信息

于 2013-11-11T10:04:42.287 回答