문제

how can I call a view from another view and pass it a model?Below in my code I would call another view at #button_cont click as showed in events.Can I simply call the new view and pass it model or exists a better way?

Below a caller view:

define(["jquery", "underscore","Handlebars","models/attore",
"text!templates/backend0.html"],
function ($, _, Handlebars,Attore,template) {

var BackendView0 = Backbone.View.extend({



    template: Handlebars.compile(template),


    events: {

        'click #button_cont': 'twitter_list',

    },



    initialize: function () {


    },

    render: function (eventName) {


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

      return this;


    },



     twitter_list: function () {
        var nome=$('#nome').val();
        var cognome=$('#cognome').val();

        var attore= new Attore({nome:nome,cognome:cognome});

   ---  Here I want call another view and pass it model attore---------
    },


  });

return BackendView0;
도움이 되었습니까?

해결책

If it will always be only the 2 views you could tell them about each other by storing a reference to another view. Then the method to pass a model could do something like this:

this.otherView.model = this.model;

This reference can be stored on the view initialization or through a setter method:

setOtherView: function(view){
    this.otherView = view;
}

다른 팁

You don't say whether your two views are separate from each other or part of a hierarchy. If the views are truly separate, then they shouldn't be "calling" each other at all. Each view should manage its own part of the page independent of any parts of the page.

If the views are part of a hierarchy (e.g. a collection view and a model view), then the parent view should be responsible for creating the child view, in which case it can pass the model as input to the constructor just like any other view.

I would do it like this:

var myNewView = new AnotherView({ model: attore });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top