简体   繁体   English

非常基本的Backbone.js问题

[英]Very Basic Backbone.js issue

I'm sure this is obvious but I am new to backbone.js and have wasted a lot of time on it. 我敢肯定这是显而易见的,但是我对bone.js还是陌生的,并且浪费了很多时间。 I'm trying to set up a very simple example with Backbone where this.name is a persons name and the program alerts 'hi' when the name is changed. 我正在尝试使用Backbone建立一个非常简单的示例,其中this.name是人员姓名,程序在更改名称时会提示“ hi”。 However when I run the code below I get the error 但是,当我运行下面的代码时,我得到了错误

Object function (){a.apply(this,arguments)} has no method 'get' 对象函数(){a.apply(this,arguments)}没有方法'get'

this.name = Backbone.Model.extend({
    initialize: function() {
      this.bind('change', function() {alert('hi')});
    },
    defaults : {
      name: 'bob'
    }

  });

console.log(this.name.get('name'));

Please let me know what I am doing wrong, this is driving me crazy :) Thanks! 请让我知道我在做什么错,这让我发疯了:)谢谢!

this.name hasn't been instantiated yet. this.name尚未实例化。 Try this instead: 尝试以下方法:

this.nameModel = Backbone.Model.extend({
  initialize: function() {
    this.bind('change', function() {alert('hi')});
  },
  defaults : {
    name: 'bob'
  }
});

// instantiate the model
this.name = new this.nameModel();
console.log(this.name.get('name'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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