I've created an ASP.Net user control that will get placed more than once inside of web page. In this control I've defined a javascript object such as:
我创建了一个ASP.Net用户控件,它将在网页中不止一次放置。在这个控件中我定义了一个javascript对象,例如:
function MyObject( options )
{
this.x = options.x;
}
MyObject.prototype.someFunction=function someFunctionF()
{
return this.x + 1;
}
In the code behind I've created MyObject in a startup script --
在后面的代码中,我在启动脚本中创建了MyObject -
var opts = { x: 99 };
var myObject = new MyObject( opts );
When a certain button in the control is pressed it will call myObject.someFunction(). Now lets say the value of x will be 99 for one control but 98 for another control. The problem here is that the var myObject will be repeated and only the last instance will matter. Surely there's a way to make the var myObject unique using some concept I've haven't run across yet. Ideas?
当按下控件中的某个按钮时,它将调用myObject.someFunction()。现在假设一个控件的x值为99,而另一个控件的值为98。这里的问题是var myObject将被重复,只有最后一个实例才重要。当然有一种方法可以使用我尚未遇到的一些概念使var myObject独一无二。想法?
Thanks,
Craig
5 个解决方案
#1
Your Javascript like this:-
你的Javascript是这样的: -
function MyObject(options) { this.x = options.x; }
MyObject.prototype.someFunction = function() { return this.x + 1; }
MyObject.create(id, options) {
if (!this._instances) this._instances = {};
return this._instances[id] = new MyObject(options);
}
MyObject.getInstance(id) { return this._instances[id]; }
Your startup javascript like this:-
你的启动javascript是这样的: -
MyObject.create(ClientID, {x: 99});
Other code that needs to use an instance (say in the client-side onclick event)
需要使用实例的其他代码(例如在客户端onclick事件中)
String.Format("onclick=\"MyObject.getInstance('{0}').someFunction()\", ClientID);
Note the low impact on the clients global namespace, only the MyObject identifier is added to the global namespace, regardless of how many instances of your control are added to the page.
请注意,对客户端全局命名空间的影响较小,只有MyObject标识符才会添加到全局命名空间,而不管您向页面添加了多少控件实例。
#2
If it is just one value, why not have the function take it as a parameter and build your onclick handler so that it puts the correct value in for each control. If it is more complex than that, then consider making options an array and, for each control, insert the correct options into the spot in the array that corresponds to each particular control. Then pass the proper index into the array into the function.
如果它只是一个值,为什么不将该函数作为参数并构建您的onclick处理程序,以便为每个控件放入正确的值。如果它比这更复杂,那么考虑将选项设置为数组,并且对于每个控件,将正确的选项插入到与每个特定控件对应的数组中的点。然后将正确的索引传递给数组到函数中。
#3
I do this by using ScriptManager.RegisterClientScriptBlock to register a string as a JavaScript block on the client side. I can then modify my script string using {0}, {1}..,{n} place holders to inject necessary ids. It depends on the structure of your code as to if this is the most elegant fashion, but it works in a pinch. You could then inject variable names using references to Me.ClientID.
我这样做是通过使用ScriptManager.RegisterClientScriptBlock在客户端将字符串注册为JavaScript块。然后,我可以使用{0},{1} ..,{n}占位符修改我的脚本字符串以注入必要的ID。这取决于你的代码的结构,如果这是最优雅的方式,但它在一个紧要关头。然后,您可以使用对Me.ClientID的引用来注入变量名称。
#4
You can make the value of "x" static and access it anywhere in the code, such as:
您可以将“x”的值设置为静态并在代码中的任何位置访问它,例如:
function MyObject( options ) { MyObject.x = options.x; }
MyObject.x = 99; // static
MyObject.prototype.someFunction = function () { return MyObject.x + 1; }
This way you can access MyObject.x anywhere in your code, even without re-instanciating MyObject.
这样,即使不重新设置MyObject,也可以在代码中的任何位置访问MyObject.x.
#5
Excellent solution Anthony. The other solutions offered were as good and I did consider them but I was looking for something a little more elegant like this solution. Thanks!
安东尼的优秀解决提供的其他解决方案同样好,我考虑过它们,但我正在寻找像这个解决方案更优雅的东西。谢谢!
#1
Your Javascript like this:-
你的Javascript是这样的: -
function MyObject(options) { this.x = options.x; }
MyObject.prototype.someFunction = function() { return this.x + 1; }
MyObject.create(id, options) {
if (!this._instances) this._instances = {};
return this._instances[id] = new MyObject(options);
}
MyObject.getInstance(id) { return this._instances[id]; }
Your startup javascript like this:-
你的启动javascript是这样的: -
MyObject.create(ClientID, {x: 99});
Other code that needs to use an instance (say in the client-side onclick event)
需要使用实例的其他代码(例如在客户端onclick事件中)
String.Format("onclick=\"MyObject.getInstance('{0}').someFunction()\", ClientID);
Note the low impact on the clients global namespace, only the MyObject identifier is added to the global namespace, regardless of how many instances of your control are added to the page.
请注意,对客户端全局命名空间的影响较小,只有MyObject标识符才会添加到全局命名空间,而不管您向页面添加了多少控件实例。
#2
If it is just one value, why not have the function take it as a parameter and build your onclick handler so that it puts the correct value in for each control. If it is more complex than that, then consider making options an array and, for each control, insert the correct options into the spot in the array that corresponds to each particular control. Then pass the proper index into the array into the function.
如果它只是一个值,为什么不将该函数作为参数并构建您的onclick处理程序,以便为每个控件放入正确的值。如果它比这更复杂,那么考虑将选项设置为数组,并且对于每个控件,将正确的选项插入到与每个特定控件对应的数组中的点。然后将正确的索引传递给数组到函数中。
#3
I do this by using ScriptManager.RegisterClientScriptBlock to register a string as a JavaScript block on the client side. I can then modify my script string using {0}, {1}..,{n} place holders to inject necessary ids. It depends on the structure of your code as to if this is the most elegant fashion, but it works in a pinch. You could then inject variable names using references to Me.ClientID.
我这样做是通过使用ScriptManager.RegisterClientScriptBlock在客户端将字符串注册为JavaScript块。然后,我可以使用{0},{1} ..,{n}占位符修改我的脚本字符串以注入必要的ID。这取决于你的代码的结构,如果这是最优雅的方式,但它在一个紧要关头。然后,您可以使用对Me.ClientID的引用来注入变量名称。
#4
You can make the value of "x" static and access it anywhere in the code, such as:
您可以将“x”的值设置为静态并在代码中的任何位置访问它,例如:
function MyObject( options ) { MyObject.x = options.x; }
MyObject.x = 99; // static
MyObject.prototype.someFunction = function () { return MyObject.x + 1; }
This way you can access MyObject.x anywhere in your code, even without re-instanciating MyObject.
这样,即使不重新设置MyObject,也可以在代码中的任何位置访问MyObject.x.
#5
Excellent solution Anthony. The other solutions offered were as good and I did consider them but I was looking for something a little more elegant like this solution. Thanks!
安东尼的优秀解决提供的其他解决方案同样好,我考虑过它们,但我正在寻找像这个解决方案更优雅的东西。谢谢!