要求。如何将模型传递给视图(骨干)

时间:2013-11-11 09:53:50

标签: javascript jquery backbone.js requirejs

我使用带有主干的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;

});

1 个答案:

答案 0 :(得分:1)

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

RewardView

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