0

(I am using pseudo-code)

I have a series of ASP.NET custom controls. Each one involves a lot of javascript, so they have corresponding .js files. They inject a line such as 'var this.clientid = new jsobject();' so every control has a javascript object on the page with the same ID as itself.

This works fine, except that some controls rely on others. For example, a CatFlap object needs to know about CatHouse. So in its constructor, it will take in the CatFlap object, a bit like this:

page.inject("var " + this.ClientID + " = new CatFlap(" + CatHouse.ClientID + ");";

however in the constructor for CatFlap, CatHouse is undefined if on the page, the 2 custom controls are in the opposite order, eg. the comes before because it hasn't constructed the item yet.

One solution is to put the declaration into document.ready(), but this fails because my control system is slightly more complicated than above, with other dependancies such as CatFlapSwitch depending on CatFlap and thus both constructors would go into document.ready() and the problem would manifest itself again.

There are many suites of tools out there that have several custom controls, does anyone know how they manage to solve this issue?

An example of the problem: http://jsfiddle.net/tckuM/

If you switch the two object declaration lines around, you get an error, I am wondering how to mitigate that.

1 Answer 1

1

Yes, document.ready or in ASP.NET AJAX, Sys.Application.add_load are event handlers that delay the execution of code, and you have to rely on that. Additionally, you have to pass external dependencies as properties, not as constructor arguments. It won't work with constructor arguments very well because of the very issue you are mentioning. You may be able to get it all to work with properties and not need document/ready/app.load events...

HTH.

Sign up to request clarification or add additional context in comments.

2 Comments

I added a JSFiddle example, the functions I make are declared as it's constructed though so I am not sure how to avoid this?
yes a lot of frameworks use properties for initialization like this, especially for dependencies when you can't control the order of code execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.