(非紧急)如何跨多个控件使用单个javascript对象 [英] (not-urgent) How do I use a single javascript object across several controls

查看:58
本文介绍了(非紧急)如何跨多个控件使用单个javascript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在这里寻找创意。



我有一个带有选项卡和多个子控件的主控件。我做了这个A:简化主控制和B:因为我需要打开/关闭一些功能,所以不要渲染这些子控件。



现在结果是我将通过javascript填充这些控件客户端。



目前,我的主控制器获取数据然后检查每个控件是否存在,检查每个控件的populateMe()函数都存在,然后将该对象传递给该控件。



为避免与子控件populateMe()函数名称混淆(我没有实际上调用我的任何函数populateMe()),我改为创建一个函数数组,每个控件都将它自己的populateMe()函数推送到。

IE:

  //  主要控件: 
var AgentPortalPopulateDetailFunctions = [];
function populateChildren(data){
$ .each(AgentPortalPopulateDetailFunctions, function ( ){
this (data);
});
}



  //  子控件: 
$( function (){
AgentPortalPopulateDetailFunctions.push( function (data){
// 填充我
});
});





这意味着我的主控制器不知道控件需要什么信息所以它每次都传递整个对象。它不是一个庞大的对象,但它不小,这个过程几乎和用户点击的过程一样快:S $



Q1:从一个函数传递这个大对象另一个问题?

Q2:还有另外一种方法可以让每个孩子控制分开吗?



任何想法都会有用:)



谢谢^ _ ^

Andy

解决方案

.each(AgentPortalPopulateDetailFunctions , function (){
this (data);
});
}



  //  子控件: 


function (){
AgentPortalPopulateDetailFunctions.push( function (data){
// 填充我
});
});





这意味着我的主控制没有了解控件需要什么信息,以便每次都传递整个对象。它不是一个庞大的对象,但它不小,这个过程几乎和用户点击的过程一样快:S $



Q1:从一个函数传递这个大对象另一个问题?

Q2:还有另外一种方法可以让每个孩子控制分开吗?



任何想法都会有用:)



谢谢^ _ ^

Andy


无论如何都没有这样的问题。在将对象传递给方法方面,没有大对象这样的东西,因为对象的引用是传递的,没有对象克隆,除非你专门开发这样的机制。当然,对象将通过引用传递给不同的代码区域来共享。



至于另一种方式,如果你解释了这个,我可以建议一个确切的最终目标是你想要实现的目标。无论如何,填充方法的数组听起来很奇怪,因为你说你为每个控件都有一个单独的填充函数。那你应该简单地使用这些功能。函数可以是任何对象的属性,包括HTML元素周围的jQuery包装器。关键在于:每个特定控件及其人口的知识在哪里以及如何以清晰的方式表达。



-SA

Hi,

I'm mostly looking for ideas, here.

I have a main control with tabs and several child controls. I have done this A: to simplify the main control and B: because I will need to turn some features on / off and so not render some of these child controls.

It now turns out that I will be populating these controls client-side via javascript.

Currently, my main control gets the data then checks that each control exists, checks that each control's populateMe() function exists, then passes that object to that control.

To avoid confusion with the child control populateMe() function names (I didn't actually call any of my functions populateMe()), I instead create an array of functions that each control pushes it's own populateMe() function to.
IE:

//Main control:
var AgentPortalPopulateDetailFunctions = [];
function populateChildren(data){
    $.each(AgentPortalPopulateDetailFunctions,function(){
        this(data);
    });
}


//a child control:
$(function () {
  AgentPortalPopulateDetailFunctions.push(function(data){
     //populate me
  });
});



This means that my main control has no idea what info the control needs so it passes the whole object each time. It's not a massive object but it's not small and this process will happen almost as fast as the user can click :S

Q1: Is passing this large object from one function to another problematic?
Q2: Is there another way I can do this that still keeps each child-control separate?

Any ideas would be useful :)

Thanks ^_^
Andy

解决方案

.each(AgentPortalPopulateDetailFunctions,function(){ this(data); }); }


//a child control:


(function () { AgentPortalPopulateDetailFunctions.push(function(data){ //populate me }); });



This means that my main control has no idea what info the control needs so it passes the whole object each time. It's not a massive object but it's not small and this process will happen almost as fast as the user can click :S

Q1: Is passing this large object from one function to another problematic?
Q2: Is there another way I can do this that still keeps each child-control separate?

Any ideas would be useful :)

Thanks ^_^
Andy


There is no such problem, whatsoever. In terms of passing objects to methods, there is no such thing as "large" object, because the references to the objects are passed, there is no object cloning, unless you specifically develop such mechanism. Of course, the object will be shared through the referenced passed to different places of code.

As to the "another way", I could advise one if you explained the exact ultimate goal of what you are trying to achieve. Anyway, and array of "populate" methods sounds weird, because you say that you have a separate "populate" function for each control. Then you should simply use the functions. A function can be a property of any object, including the jQuery wrapper around an HTML element. The key here: where is the knowledge on each specific control and its population and how it can be expressed in a clear way.

—SA


这篇关于(非紧急)如何跨多个控件使用单个javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆