require.js在模型中传递Object [object Object]没有方法'fetch'

时间:2013-11-11 10:29:36

标签: javascript jquery backbone.js requirejs

我的视图中出现错误,其中var campaign = new CampaginModel()没有获得模型。

使用require.js + backbone:

错误:

未捕获TypeError:对象[object Object]没有方法'fetch'

当我控制对象console.log(CampaginModel)时,我得到function (){return i.apply(this,arguments)}这是完全错误的,因为它应该是模型的一个实例。

我在这里做错了什么?

模型(models / CampaginModel):

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

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

  return CampaginModel;

});

查看:

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


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



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

                campaign.fetch({
                    success: function(campaign){

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


  return CampaginView;

});

main.js:

require.config({

  shim: {


    underscore: {
      exports: '_'
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone',
      init: function (_, $) { Backbone.$ = $; return Backbone; }


    }
  },
  paths: {
    jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min',
    underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min',
    backbone: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min',
    templates: '/templates/'
  }

});

require([
  'app'
], function(App){

  App.initialize();

});

1 个答案:

答案 0 :(得分:1)

您定义视图的定义不正确,因为您定义了七个参数,但只将五个参数传递给该函数。因此,当您实例化CampaginModel时,您实际拥有的是视图views/FriendRewardView的实例。所以改变你的定义如下:

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

});

干杯。