将值从一个页面转移到另一个页面主干Js

时间:2013-02-12 08:07:43

标签: javascript backbone.js global-variables

我想将值表单弹出视图传递给父视图如何使用Backbone JS

我完成了像这样的代码

用于弹出视图

loadPopupGeoBox: function () {         
  var newUserlist = new TripsdetailsModel(); 
  selectModel = newUserlist;
  var that = this;

  require(['views/searchlocationek'], function (view) {
    var view = new view({ model: selectModel}); 
    view.open();
    that.addview = view;
  });

  $('#popup_geocode').fadeIn("slow");
  $("#container").css({ 
    "opacity": "0.3"
  });
},

从弹出页面提交按钮我们已完成以下代码

clickClose: function() {
  require(['views/trip'], function (view) {  
    var view1 = new view();
    view1.openfilter(final); 
  }); 

  $('#popup_geocode').fadeOut("slow");
  $("#container").css({ // this is just for style       
    "opacity": "1"
  });
},

如何在父视图中获取“final”的值?

1 个答案:

答案 0 :(得分:1)

我不确定最佳做法,但有一种方法是,将this引用从父视图传递到popup页面:

loadPopupGeoBox: function () {         

  // existing code

  var $this = this;

  require(['views/searchlocationek'], function (view) {
    var view = new view({ 
      model: selectModel,
      parent_view: $this
    });   
  });

  // existing code

},

popup视图中:

clickClose: function() {
  // storing this reference to be used in the require call back
  var $this = this;
  require(['views/trip'], function (view) {  
    // existing code

    // set the value of the final variable on the parent_view with
    $this.options.parent_view["final_var"] = final;

  }); 

  // existing code
},

执行clickClose后,final视图中parent的{​​{1}}值将会显示{/ 1}}。