简体   繁体   English

如何使用Backbone js中的函数从视图调用另一个视图

[英]How to call another View from a view using a function in Backbone js

Is there an underscore or backbone function I can use to call a view from another view? 我可以使用下划线或主干功能从另一个视图调用视图吗? I only want to display info using the History view but I am having trouble calling it. 我只想使用“历史记录”视图显示信息,但调用时遇到问题。

var CityModel = Backbone.Model.extend({
defaults: {
    city: null,
    nickName: null,
    founded: null
}
});
var StudentCollection = Backbone.Collection.extend({
   Model: CityModel
});

// This outputs the data when there is a click event
var History = Backbone.View.extend({
    tagName: 'li',
    template: _.template("<h2><%= city %></h2>"),
    initialize: function(){
        this.render();
    },
    render: function(){
        this.$el.html(this.template(this.model));
        $('.History').append(this.$el);
        return this;
    }
});

I want to call the History View from the CityView function click event which I named displayFunction 我想从名为DisplayFunction的CityView函数click事件中调用历史视图

var myCityTemplate = '<h1 class= "displayInfo" name= "showCity"><%= city %></h1>';
var CityView = Backbone.View.extend({
    tagName: "li",
    template: _.template(myCityTemplate),
    initialize: function(){
        this.listenTo(this.model, 'change', this.render);
    },
    render: function(){
        this.$el.html(this.template(this.model.toJSON()));
        return this;
    },
    events:{
        'click .displayInfo':'displayFunction'
    },
    displayFunction: function(){
        // This is to find where city lives, check out the console.log without the innerHTML
        // console.log(this.$el.find('h1[name= "showCity"]')[0].innerHTML);
        CityName = this.$el.find('h1[name= "showCity"]')[0].innerHTML;
        console.log(Cities.where({city: CityName}));
        var myCity = Cities.where({city: CityName});
        var CityHistory = new History({model: myCity});
    }
});

I call the History view using the model mycity, which I define once I get data from the click event. 我使用模型mycity调用“历史记录”视图,该模型是在我从click事件中获取数据后定义的。 I do have data when I console.log the Cities. 在console.log城市时,我确实有数据。

var CityListView = Backbone.View.extend({
    el: ".cities",
    initialize: function(){
        this.render();
    },
    render: function(){
        this.$el.empty();
        this.collection.each(function(places){
            var MetropolisViewModel = new CityView({model: places});
            this.$el.append(MetropolisViewModel.render().$el);
        }, this);
    }
});
var city1 = new CityModel({city: "Anaheim", nickName: "Disney", founded: "1840"});
var city2 = new CityModel({city: "Las Vegas", nickName: "Sinners",   founded: "1889"});
var city3 = new CityModel({city: "New York", nickName: "City of hope", founded: "1750"});
var city4 = new CityModel({city: "Palm Springs", nickName: "Very hot", founded: "1849"});
var Cities = new StudentCollection([city1, city2, city3, city4]);
var myCityList = new CityListView({collection: Cities});

Everything works, its only that issue of calling the History view from the cityView which doesn't seem to work. 一切正常,只有从cityView调用History视图的问题似乎不起作用。 My goal is use History view as a way of only displaying data from each city to my HTML. 我的目标是使用“历史记录”视图作为一种仅显示每个城市到HTML的数据的方式。

I figured it out, the problem was that when I use 我想通了,问题是当我使用

this.$el.html(this.template(this.model) 这一点。$ el.html(this.template(this.model)

in the History View, the data that I will be displaying, is in a different format as compared to the data that gets displayed from the CityView, which I use 在“历史记录视图”中,我将要显示的数据与从“城市视图”中显示的数据相比格式不同。

this.$el.html(this.template(this.model.toJSON())); 这$ el.html(this.template(this.model.toJSON()));

to display the data. 显示数据。 So the problem wasn't about calling the view but displaying the data differently. 因此,问题不在于调用视图,而是以不同的方式显示数据。 Differently because when I get information from a model the format is different enough to have issues displaying that data. 不同的是,当我从模型中获取信息时,格式差异很大,以至于显示该数据时出现问题。 Thus, I used this.$el.html(this.template(this.model[0].attributes)); 因此,我使用了this。$ el.html(this.template(this.model [0] .attributes)); in my History view to properly output data. 在“历史记录”视图中以正确输出数据。

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

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