Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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

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,我使用backbone构建客户端应用程序,我试图做的是每次用户单击时显示一个笑话。获取事件笑话:以下是我的backbone应用程序代码: JokeModel = Backbone.Model.extend({ url: '/jokes' initialize: function() { this.fetch(); } }); JokeView = Backbone.View.extend({ template: _.template("<p>&l

我使用backbone构建客户端应用程序,我试图做的是每次用户单击时显示一个笑话。获取事件笑话:以下是我的backbone应用程序代码:

JokeModel = Backbone.Model.extend({
   url: '/jokes'
   initialize: function() {
      this.fetch();
  }
});

JokeView = Backbone.View.extend({
    template: _.template("<p><%= joke %></p>")
    events: {
      "click .get_joke" : "render"
  } 
    render: function() {
       var newJoke = new JokeModel;
       $(this.el).html(this.template(newJoke.toJSON()));
  }
});

newJokeView = new JokeView;
JokeModel=Backbone.Model.extend({
url:“/笑话”
初始化:函数(){
this.fetch();
}
});
JokeView=Backbone.View.extend({
模板:【模板(“

”) 活动:{ “单击。获取”:“渲染” } render:function(){ var newJoke=newjokemodel; $(this.el).html(this.template(newJoke.toJSON()); } }); newJokeView=新的JokeView;
单击时出现问题。get_笑话它不向视图呈现笑话,我知道模型已被获取,因为我使用console.log进行了检查,但它说笑话尚未定义,但我不知道问题在哪里。谢谢

请尝试以下操作:

JokeModel = Backbone.Model.extend({
   url: '/jokes'
   parse : function(data) {
      console.log(data);
      return data;
   }
   initialize: function() {
      this.fetch();
  }
});
newJoke.toJSON()
我还将注销以下内容:

JokeModel = Backbone.Model.extend({
   url: '/jokes'
   parse : function(data) {
      console.log(data);
      return data;
   }
   initialize: function() {
      this.fetch();
  }
});
newJoke.toJSON()
以查看实际要渲染的内容

尝试以下操作:

JokeModel = Backbone.Model.extend({
   url: '/jokes'
   parse : function(data) {
      console.log(data);
      return data;
   }
   initialize: function() {
      this.fetch();
  }
});
newJoke.toJSON()
我还将注销以下内容:

JokeModel = Backbone.Model.extend({
   url: '/jokes'
   parse : function(data) {
      console.log(data);
      return data;
   }
   initialize: function() {
      this.fetch();
  }
});
newJoke.toJSON()

以查看实际要渲染的内容

首先,你不能相信关于复杂对象的
console.log
sai:

发生的情况是,
joke.fetch()
是异步的,当您调用
jokeView.render()
时,模型仍然没有准备好

您应该稍微修改一下您的体系结构,并为每个笑话指定一个适当的视图,这样您就可以为每个笑话提供一个视图,在需要时显示它

// code simplified an not tested
JokeModel = Backbone.Model.extend({
   url: '/jokes'
});

// This View is new and it is taking care only for the common event "click .get_joke"
// which is not related with any Joke in particular
// This View should be in charge of the JokesCollection 
// but I don't want to make things more complicate
JokeControls = Backbone.View.extend({
  events: {
    "click .get_joke" : "render"
  }, 

  getJoke: function(){
    var joke = new JokeModel();
    var view = new JokeView({ model: joke, el: this.$el.find( ".joke-wrapper" ) });
    joke.fetch();
  },
});


// This is the View that is related with unique Joke
JokeView = Backbone.View.extend({
    template: _.template("<p><%= joke %></p>"),

    initialize: function(){
      // see how it shows itself when the Model is ready
      this.model.on( "change", this.render, this );
    },

    render: function() {
       this.$el.html(this.template(this.model.toJSON()));
    }
});

// Activate the Joke Controls
newJokeControls = new JokeControls({ el: "#joke-controls" });
//未测试简化的代码
JokeModel=Backbone.Model.extend({
url:“/笑话”
});
//此视图是新的,它只关注常见事件“click.get\u笑话”
//这与任何笑话都没有关系
//此视图应该负责JokeCollection
//但我不想让事情变得更复杂
JokeControl=Backbone.View.extend({
活动:{
“单击。获取”:“渲染”
}, 
getJoke:function(){
var joke=new JokeModel();
var view=new JokeView({model:joke,el:this.$el.find(“.joke包装”)});
取笑;
},
});
//这是与独特笑话有关的观点
JokeView=Backbone.View.extend({
模板:【模板(“

”), 初始化:函数(){ //当模型准备就绪时,查看它是如何显示的 this.model.on(“change”,this.render,this); }, render:function(){ this.el.html(this.template(this.model.toJSON()); } }); //激活笑话控件 NewJokeControl=新的JokeControl({el:#笑话控件“});
首先,你不能相信控制台上的内容。日志是关于复杂对象的:

发生的情况是,
joke.fetch()
是异步的,当您调用
jokeView.render()
时,模型仍然没有准备好

您应该稍微修改一下您的体系结构,并为每个笑话指定一个适当的视图,这样您就可以为每个笑话提供一个视图,在需要时显示它

// code simplified an not tested
JokeModel = Backbone.Model.extend({
   url: '/jokes'
});

// This View is new and it is taking care only for the common event "click .get_joke"
// which is not related with any Joke in particular
// This View should be in charge of the JokesCollection 
// but I don't want to make things more complicate
JokeControls = Backbone.View.extend({
  events: {
    "click .get_joke" : "render"
  }, 

  getJoke: function(){
    var joke = new JokeModel();
    var view = new JokeView({ model: joke, el: this.$el.find( ".joke-wrapper" ) });
    joke.fetch();
  },
});


// This is the View that is related with unique Joke
JokeView = Backbone.View.extend({
    template: _.template("<p><%= joke %></p>"),

    initialize: function(){
      // see how it shows itself when the Model is ready
      this.model.on( "change", this.render, this );
    },

    render: function() {
       this.$el.html(this.template(this.model.toJSON()));
    }
});

// Activate the Joke Controls
newJokeControls = new JokeControls({ el: "#joke-controls" });
//未测试简化的代码
JokeModel=Backbone.Model.extend({
url:“/笑话”
});
//此视图是新的,它只关注常见事件“click.get\u笑话”
//这与任何笑话都没有关系
//此视图应该负责JokeCollection
//但我不想让事情变得更复杂
JokeControl=Backbone.View.extend({
活动:{
“单击。获取”:“渲染”
}, 
getJoke:function(){
var joke=new JokeModel();
var view=new JokeView({model:joke,el:this.$el.find(“.joke包装”)});
取笑;
},
});
//这是与独特笑话有关的观点
JokeView=Backbone.View.extend({
模板:【模板(“

”), 初始化:函数(){ //当模型准备就绪时,查看它是如何显示的 this.model.on(“change”,this.render,this); }, render:function(){ this.el.html(this.template(this.model.toJSON()); } }); //激活笑话控件 NewJokeControl=新的JokeControl({el:#笑话控件“});