简体   繁体   English

在骨干.js中使用视图

[英]Using views in backbone.js

I'm currently learning backbone.js and have a little problem. 我目前正在学习ribs.js,有一个小问题。 I dont' quite get how the view works. 我不太了解视图的工作原理。 I have created a model, a collection, and another model that again contains the collection: 我创建了一个模型,一个集合以及另一个再次包含该集合的模型:

Sensor = Backbone.Model.extend({
    defaults: {
        channel: '',
        name: '',
        temperature: 0,
        tempMin: 0,
        tempMax: 0
    }
});

SensorList = Backbone.Collection.extend({
    model: Sensor
});

Now I created a view, so I am able to render the sensor collection with handlebar.js template: 现在,我创建了一个视图,因此可以使用handlebar.js模板渲染传感器集合:

TemperatureView = Backbone.View.extend({

    initialize: function() {
        this.render();
    },

    render: function(eventName) {
        var source = $('#sensor-list-template').html();
        var template = Handlebars.compile(source);
        var html = template(this.collection.toJSON());

        this.$el.html(html);
    } 
});

Now I want to load some data and render the information. 现在,我想加载一些数据并呈现信息。 But I don't know how to get the data into my view...I tried this: 但是我不知道如何将数据导入我的视图...

$(document).ready(function() {
    var temps = new TemperatureRequest();
    temps.fetch({
        success: function() {
            console.log(temps);
            var test = temps.get("sensors");
            console.log(test);

            var tempView = new TemperatureView({
                collection: test
            });

        }
    });
});

The data is fetched correctly. 数据已正确提取。 I have a collection of sensors. 我有一组传感器。 And now I want to pass them to the view so it is getting rendered....but I don't understand how this is done.. pls help! 现在,我想将它们传递给视图,以便呈现它。...但是我不知道该如何完成.. 请帮助!

Since you are passing the collection to the view while creating it, you can access the same using this.collection inside your view anywhere. 由于在创建集合时将其传递给视图,因此可以在视图内部的任何位置使用this.collection访问该集合。

var tempView = new TemperatureView({
            collection: test
});

More over you have added the render function inside your initialize , it automatically calls the render function.Inside the render it fetches the collection and since your template needs only json object you are converting your collection it to json array objects.Templates takes care of appending the values to html. 此外,您在initialize中添加了render函数,它会自动调用render函数。在render内部,它获取集合,并且由于模板仅需要json对象,因此您可以将集合将其转换为json数组对象。 html的值。

If you want to add automatic view render to happen whenever the collection removes a model or adds a model into it you can add a listener and callback function to it 如果要在集合删除模型或向其中添加模型时添加自动视图渲染,则可以向其添加侦听器和回调函数

 initialize : function(){
    console.log("initializing view");
    this.collection.on('add', this.render, this);
    this.collection.on('reset', this.render, this);
    this.render();
  }

I just got it. 我知道了 Took me a while and I have definitly some reading to do. 花了我一段时间,我确实已经做了一些阅读工作。

There were several problems. 有几个问题。 First of all I have to overwrite the parse function, so the collection is stored correctly in my model: 首先,我必须覆盖parse函数,因此该集合正确存储在我的模型中:

TemperatureRequest = Backbone.Model.extend({
    urlRoot: '/temperatures',
    defaults: {
        timestamp: '',
        logfile: '',
        sensorList: new SensorList()
    },
    parse: function(response) {
        response.sensorList = new SensorList(response.sensors);

        return response;
    },

    success: function(response) {
        console.log('success');

    }
});

In my view I know add the listen to events as suggested and also fetch the data within the initialize function to get rid of the success callback: 在我看来,我知道按照建议添加监听事件,并且还可以在initialize函数中获取数据以摆脱成功回调:

TemperatureView = Backbone.View.extend({

    el: '#temperatures',

    initialize: function() {
        this.listenTo(this.model, 'reset', this.render);
        this.listenTo(this.model, 'change', this.render);
        this.listenTo(this.model, 'add', this.render);

        this.model.fetch();
    },

    render: function(eventName) {
        var list = this.model.get('sensorList');
        console.log(list.toJSON());
        var source = $('#sensor-list-template').html();
        var template = Handlebars.compile(source);
        var html = template(list.toJSON());

        this.$el.html(html);
        this.renderTimestamp();
    },
    renderTimestamp: function() {
        var tsText = $("<p></p>").addClass("text-right");
        var timestamp = $("<div></div>").addClass("col-sm-4 col-sm-offset-8").append(tsText);
        tsText.text(this.model.get('timestamp'));
        $('#timestamp').append(timestamp);
    }
});

now I can do this to render the data: 现在,我可以执行此操作以呈现数据:

$(document).ready(function() {
    var temps = new TemperatureRequest();
    var tempsView = new TemperatureView({
        model: temps
    });
});

Instead of passing the collection to the view I pass the model to it and fetch the data inside of the initialize function. 我没有将集合传递给视图,而是将模型传递给它,并在initialize函数内部获取数据。

What I still don't understand is when I have to use "this" and when I have to use _bindAll... 我仍然不了解的是何时必须使用“ this”以及何时必须使用_bindAll ...

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

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