简体   繁体   English

如何从其他视图访问变量?

[英]How do I access a variable from a different view?

How do I access a variable from a different view? 如何从其他视图访问变量?

I want to access selectedColor in another view: 我想在另一个视图中访问selectedColor

    'onClickColor': function(e) {

        var view = this,
            selectedColor = $(e.currentTarget).data('color'), //$(this).data('color');
            style = $('<style>.highlight { color: ' + selectedColor +'; margin-left: 4px;}</style>');

        $('.initial').css('color', selectedColor);
        $('.highlight').css('color', selectedColor);
        $('html > head').append(style);
        //view.canvasColorPick(e);
    },

And then in another view, I want to pass the variable selectedColor using ajax on a form submission. 然后在另一个视图中,我想在表单提交时使用ajax传递变量selectedColor。

     'formCollect' : function (e) { 

        var view = this;
        var fname = $('#your_name').val();
        var email = $('#email').val();
        var state = $('#state').val();
        var color = selectedColor;
        var url = 'services/users/add?name='+fname+'&email='+email+'&state='+state+'&color='+color+'';
        $.get(url);
    },

It should be the best way – to subscribe one view to events of another one. 这应该是最好的方法–将一个视图订阅到另一个视图的事件。 Since you did not provide the scope your views was instantiated I assume both of them are in the global scope. 由于您未提供范围,因此您实例化了视图,因此我认为它们都在全局范围内。 If you don't understand what scope mean please read this: What is the scope of variables in JavaScript? 如果您不了解范围的含义,请阅读以下内容: JavaScript中变量的范围是什么?

So assume your first view instantiated like this: firstView = new FirstView(); 因此,假设您的第一个视图是这样实例化的: firstView = new FirstView(); . The second one is secondView = new SecondView(); 第二个是secondView = new SecondView(); .

When the color changed in firstView trigger an event: this.trigger( "Color changed", {newColor: "black"} ); 当firstView中的颜色发生更改时,将触发一个事件: this.trigger( "Color changed", {newColor: "black"} ); . Please make sure that both views was instantiated before this event triggered. 请确保在触发此事件之前实例化两个视图。

Now you need to subscribe secondView on the event of firstView: secondView.listenTo( firstView, "Color changed", secondView.handleColorChanged ); 现在,您需要在firstView事件上订阅secondView: secondView.listenTo( firstView, "Color changed", secondView.handleColorChanged );

Here handleColorChanged is the event handler which will get event params as arguments. 这里的handleColorChanged是事件处理程序,它将获取事件参数作为参数。

Now alltogeather: 现在Alltogeather:

var FirstView = Backbone.view.extend({
  /** your casual code here */
  'onClickColor': function(e) {
     /** your code here */
     this.trigger( "Color changed", { newColor: "black" });
  }
});

var SecondView = Backbone.view.extend({
  /** your casual code here */
  'handleColorChanged': function( eventData ) {
     console.log( eventData );
  }
});

var firstView = new FirstView(),
    secnodView = new SecondView();
secondView.listenTo( firstView, "Color changed", secondView.handleColorChanged );

This is not the only one way to solve your task. 这不是解决任务的唯一方法。 But this is the way to separate views from each other. 但这是将视图彼此分离的方法。 No one view knows about another one what is mostly needed in big applications, makes easy debugging process and so on. 没有人知道另一种观点,这是大型应用程序中最需要的,它使调试过程变得容易等。

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

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