Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干-视图没有方法';获取';_Javascript_Backbone.js - Fatal编程技术网

Javascript 主干-视图没有方法';获取';

Javascript 主干-视图没有方法';获取';,javascript,backbone.js,Javascript,Backbone.js,大家好 我刚开始学习主干,我的第一个代码有问题。我制作的示例代码有一个按钮,单击该按钮后,将添加一段代码(视图),该代码应该从模型中获取一些默认值。但是我收到一条消息,上面说:对象函数(){returni.apply(this,arguments)}没有方法“get”。这是我的密码: var app = {}; // the model app.Answer = Backbone.Model.extend({ defaults: { title: 'Your a

大家好

我刚开始学习主干,我的第一个代码有问题。我制作的示例代码有一个按钮,单击该按钮后,将添加一段代码(视图),该代码应该从模型中获取一些默认值。但是我收到一条消息,上面说:
对象函数(){returni.apply(this,arguments)}没有方法“get”
。这是我的密码:

  var app = {};

  // the model
  app.Answer = Backbone.Model.extend({
    defaults: {
      title: 'Your answer here',
      isAnswer: false
    }
  });

  //the collection
  app.AnswerList = Backbone.Collection.extend({
    model: app.Answer,
    localStorage: new Store("backbone-answers")
  });

  //the view that is added when the button is clicked    
  app.AnswerView = Backbone.View.extend({
    tagName: 'li',
    // template: _.template($('#answer-template').html()),
    template: _.template('<%= title %>: <%= isAnswer %>'),

    events: {
      'click button.destroy': 'remove',
    },

    initialize: function(){
      _.bindAll(this, 'render','remove');
      // this.model.bind('change', this.render());
      this.render();
    },
    render: function(){
      $(this.el).html(this.template({title: this.model.get('title'), isAnswer: this.model.get('isAnswer')}));
      return this;
    },
    remove: function(){
      $(this.el).destroy();
    }
  });

  // the main view
  app.AppView = Backbone.View.extend({
    el: $('#body'),

    events: {
      'click button#insert-button': 'addAnswer'
    },

    initialize: function(){
      _.bindAll(this, 'addAnswer');
      this.ul = $('#content');
      this.collection = new app.AnswerList();
      console.log('initialize');
    },
    addAnswer: function(){
      console.log('append');
      var newAnswer = new app.AnswerView({model: app.Answer});
      $('ul',this.el).append(newAnswer);
    }

  });

  app.appView = new app.AppView();
var-app={};
//模型
app.Answer=Backbone.Model.extend({
默认值:{
标题:“你的答案在这里”,
回答:错
}
});
//藏品
app.AnswerList=Backbone.Collection.extend({
型号:app.Answer,
本地存储:新存储(“主干回答”)
});
//单击按钮时添加的视图
app.AnswerView=Backbone.View.extend({
标记名:“li”,
//模板:35;.template($('#答案模板').html()),
模板:\.template(“:”),
活动:{
'单击按钮。销毁':'删除',
},
初始化:函数(){
_.bindAll(这是“呈现”、“移除”);
//this.model.bind('change',this.render());
这个。render();
},
render:function(){
$(this.el).html(this.template({title:this.model.get('title'),isAnswer:this.model.get('isAnswer')}));
归还这个;
},
删除:函数(){
$(this.el).destroy();
}
});
//主要观点
app.AppView=Backbone.View.extend({
el:$('正文'),
活动:{
“单击按钮#插入按钮”:“添加答案”
},
初始化:函数(){
_.bindAll(这是“addAnswer”);
this.ul=$(“#content”);
this.collection=new app.AnswerList();
log('initialize');
},
addAnswer:function(){
log('append');
var newAnswer=newapp.AnswerView({model:app.Answer});
$('ul',this.el).append(newAnswer);
}
});
app.appView=新建app.appView();
我是个脊梁骨方面的新手,所以我的问题是:我做错了什么

谢谢你的帮助

尝试使用此代码。 创建新视图时,必须实例化模型

var app = {};

  // the model
  app.Answer = Backbone.Model.extend({
    defaults: {
      title: 'Your answer here',
      isAnswer: false
    }
  });

  //the collection
  app.AnswerList = Backbone.Collection.extend({
    model: app.Answer,
    localStorage: new Store("backbone-answers")
  });

  //the view that is added when the button is clicked    
  app.AnswerView = Backbone.View.extend({
    tagName: 'li',
    // template: _.template($('#answer-template').html()),
    template: _.template('<%= title %>: <%= isAnswer %>'),

    events: {
      'click button.destroy': 'remove',
    },

    initialize: function(){
      _.bindAll(this, 'render','remove');
      // this.model.bind('change', this.render());
      this.render();
    },
    render: function(){
      $(this.el).html(this.template({title: this.model.get('title'), isAnswer: this.model.get('isAnswer')}));
      return this;
    },
    remove: function(){
      $(this.el).destroy();
    }
  });

  // the main view
  app.AppView = Backbone.View.extend({
    el: $('#body'),

    events: {
      'click button#insert-button': 'addAnswer'
    },

    initialize: function(){
      _.bindAll(this, 'addAnswer');
      this.ul = $('#content');
      this.collection = new app.AnswerList();
      console.log('initialize');
    },
    addAnswer: function(){
      console.log('append');
      var newAnswer = new app.AnswerView({model: new app.Answer()});
      $('ul',this.el).append(newAnswer);
    }

  });

  app.appView = new app.AppView();
var-app={};
//模型
app.Answer=Backbone.Model.extend({
默认值:{
标题:“你的答案在这里”,
回答:错
}
});
//藏品
app.AnswerList=Backbone.Collection.extend({
型号:app.Answer,
本地存储:新存储(“主干回答”)
});
//单击按钮时添加的视图
app.AnswerView=Backbone.View.extend({
标记名:“li”,
//模板:35;.template($('#答案模板').html()),
模板:\.template(“:”),
活动:{
'单击按钮。销毁':'删除',
},
初始化:函数(){
_.bindAll(这是“呈现”、“移除”);
//this.model.bind('change',this.render());
这个。render();
},
render:function(){
$(this.el).html(this.template({title:this.model.get('title'),isAnswer:this.model.get('isAnswer')}));
归还这个;
},
删除:函数(){
$(this.el).destroy();
}
});
//主要观点
app.AppView=Backbone.View.extend({
el:$('正文'),
活动:{
“单击按钮#插入按钮”:“添加答案”
},
初始化:函数(){
_.bindAll(这是“addAnswer”);
this.ul=$(“#content”);
this.collection=new app.AnswerList();
log('initialize');
},
addAnswer:function(){
log('append');
var newAnswer=new app.AnswerView({model:new app.Answer()});
$('ul',this.el).append(newAnswer);
}
});
app.appView=新建app.appView();
试试看


您传递给应用程序的模型。AnswerView实例化应该是应用程序的实例。Answer模型,而不是对构造函数的引用。您好,@kinakuta,谢谢您的提示!您说过,这部分:
var newAnswer=newapp.AnswerView({model:app.Answer}),对吗?如果我只是把
var newAnswer=new-app.AnswerView(),我得到了这个错误:
无法调用未定义的方法“get”
。但是如果我把
var newAnswer=newapp.AnswerView({model:newapp.Answer})放进去,我没有收到任何错误,但是没有附加视图。你知道为什么吗?谢谢,瓦迪姆!通过你的例子,我也明白了@kinakuta的意思!在将模型添加到视图之前,我没有创建模型的实例。我也看到了@Sebastian代码,但通过分析这两种方法,我看到您的示例为对象返回了一个正确的
cid
(客户机id)序列(
cid:view3
cid:view4
cid:view5
),而@Sebastian方法返回一个“跳转”数字的序列(
cid:view3
cid:view5
cid:view7
)。我不知道这样做的后果,但我更喜欢这种方法,它的编号正确。谢谢你的回答,@Sebastian!尽管这样做有效,但我发现在视图之外实例化模型,然后调用它(如@Vadim方法),返回正确数量的
cid
,我更喜欢它。
var answer = new app.Answer();
var newAnswer = new app.AnswerView({model: answer});