简体   繁体   English

将JSON数据从Nodejs Express发送到Backbone

[英]Sending JSON data from Nodejs Express to Backbone

I am trying to send some JSON data from express to Backbone model. 我正在尝试从Express向Backbone模型发送一些JSON数据。

Update 更新
console.log(response) in parse function of Model prints the values as {"version":"1.0","balance":"80.0"} 模型的parse functionconsole.log(response)将值打印为{"version":"1.0","balance":"80.0"}

console.log(this.model) in the render() function of View gives {} View的render()函数中的console.log(this.model)提供{}


Server Side Node JS 服务器端节点JS

var express = require('express');

var app = express();

app.listen(3000);

    app.get('/getInfo', function(req, res){
        //res.setHeader('Content-Type', 'application/json');
        res.json({version: "1.0", balance: "80.0"});
    });

On Node JS side I have tried following: 在Node JS方面,我尝试了以下操作:

app.get('/getInfo', function(req, res){
    res.setHeader('Content-Type', 'application/json');
    res.send({version: "1.0", balance: "80.0"});
 });

Backbone Model 骨干模型

var Bitcoin = Backbone.Model.extend({
    url:'http://localhost:3000/getInfo',
    parse: function(response) {
        console.log(JSON.stringify(response));
        return response;
    }
});

var info = new Bitcoin ();
info.fetch();

It works fine if I change it to var info = new Bitcoin ({version: "1.0", balance: "80.0"}); 如果我将其更改为var info = new Bitcoin ({version: "1.0", balance: "80.0"});它将正常工作var info = new Bitcoin ({version: "1.0", balance: "80.0"});

Backbone View 骨干视图

var BitcoinView = Backbone.View.extend({
    id:'info',
    class:'bitcoin',
    template: _.template('<span> <%= balance %> </span>'+
                         '<span><%= version %></span>'),
    render: function() {
            console.log(JSON.stringify(this.model));
        var attributes = this.model.toJSON();
        this.$el.html(this.template(attributes));
    }
});

var bitcoinView = new BitcoinView({model: info});
bitcoinView.render();

$('#app').html(bitcoinView.el);

Console 安慰

Uncaught ReferenceError: balance is not defined  (from View)
XHR finished loading: "http://localhost:3000/getInfo". 
{"version":"1.0","balance":"80.0"}  (from parse function)

You are likely trying to render a model that has no values for certain attributes. 您可能试图渲染没有某些属性值的模型。 The template of your view expects these attributes to be present. 您的视图模板期望这些属性存在。 Add defaults to your model to prevent the template method from causing an error. 向模型添加defaults以防止template方法引起错误。

var Bitcoin = Backbone.Model.extend({
    url:'http://localhost:3000/getInfo',
    parse: function(response) {
        console.log(JSON.stringify(response));
        return response;
    },
    defaults: {
        balance: "",
        version:""
    }   
});

In your view you bind the render to the change event of your model. 在您的视图中,将render绑定到模型的change事件。 This way the view will rerender when the model changes (the data is fetched). 这样,当模型更改(获取数据)时,视图将重新呈现。

this.model.bind("change", this.render, this);

Alternativaly, you need to make sure the data is fetched before rendering the view. 或者,您需要确保在渲染视图之前已获取数据。 You can use the callback function of the fetch function . 您可以使用fetch函数回调函数

info.fetch({
   success: function(){
        //create view here
    }
});

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

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