简体   繁体   English

如何在骨干.js中获取后查看模型数据

[英]How do I view model data after fetch in backbone.js

I'm kind of new to backbone coming from knockout.js and I'm trying to get over this simple hump. 我是来自strockout.js的骨干新手,我正试图克服这个简单的难题。 I have this code: 我有以下代码:

$(function(){
    window.Student = Backbone.Model;

    window.Students = Backbone.Collection.extend({
         model: Student,
         url: 'test.php'
     });

    window.students = new Students();
    window.AppView = Backbone.View.extend({
        el: $('#container'),
        initialize: function() {
            Students.bind('reset', this.render);
        },
        render: function(){
            console.log(Students.toJSON());
        }
    });

    window.appview = new AppView();

 $('#test').click(function(){
     //var students = new Students();
         students.fetch();
        var q = students.toJSON();
        console.log(q);
    /*    
        students.create({
            name: 'John',
            grade: 'A'
        }); */
    })
});

My server sends the following JSON: 我的服务器发送以下JSON:

 [{"id": "1233","name": "Karen","grade": "C"},{"id": "1234","name": "Susan", "grade": "F"}]

When I click the button and look at the console in Chrome, I see: 当我单击按钮并查看Chrome中的控制台时,我看到:

First Click: 首次点击:

[] - Corrected -just an empty array

Second Click: 第二次点击:

[
Object
  grade: "C"
  id: "1233"
  name: "Karen"
  __proto__: Object
, 
Object
  grade: "F"
  id: "1234"
  name: "Susan"
  __proto__: Object
]

First question is why does it take two clicks? 第一个问题是为什么需要两次点击? Second: How can I simply assign/bind the grade(both as a collection and by id) to a textbox, <li> or other UI element(better yet make it observable when ajax pops in). 第二:如何简单地将等级(既作为集合又通过ID)分配/绑定到文本框, <li>或其他UI元素(最好是在弹出ajax时使其可见)。

The console message you are seeing is from the click event handler. 您看到的控制台消息来自click事件处理程序。 The console message from the render method never gets called. 来自render方法的控制台消息永远不会被调用。

You don't see anything in the first log message because fetch is an asynchronous method, so when you call toJSON right after fetch , the collection hasn't been populated from the fetch method yet. 您不会在第一条日志消息中看到任何内容,因为fetch是一个异步方法,因此当您在fetch之后立即调用toJSON时,尚未从fetch方法中填充该集合。

There are a couple changes you need to make to your code to get it work as expected. 您需要对代码进行一些更改才能使其按预期工作。

First, you need to pass in the collection when you instantiate the view 首先,您需要在实例化视图时传递集合

//original
window.appview = new AppView();
//new
window.appview = new AppView({ collection: window.students });

Then within the view you need to bind to the reset event on the collection that was passed in to the constructor. 然后,在视图中,您需要绑定到传递给构造函数的集合上的reset事件。 (You have to bind to an instantiated object, and not the definition of an object like you were originally doing) (您必须绑定到实例化的对象,而不是像最初那样绑定对象的定义)

    window.AppView = Backbone.View.extend({
        el: $('#container'),
        initialize: function() {
            _.bindAll(this, 'render');
            this.collection.bind('reset', this.render);
        },
        render: function(){
            console.log(this.collection.toJSON());
        }
    });

Now comment out the console log message in your click event, then you will only have to click once, and you will see a console log message from the render method. 现在,在您的click事件中注释掉控制台日志消息,然后只需单击一次,您将看到来自render方法的控制台日志消息。

 $('#test').click(function(){
     //var students = new Students();
        students.fetch();
        //var q = students.toJSON();
        //console.log(q);
    /*  
        students.create({
            name: 'John',
            grade: 'A'
        }); */
    })

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

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