Everyday inline use |
Important CJC.js must be included in the source stack before it is actually used.
This can be a gotcha as you might be doing things like loading configurations very early on.
|
Convert anything to JSON |
myStr = CJC.ToJ(Anything)
An alias CJC.toJSON() is available.
|
Create an object from JSON |
myThing = CJC.FromJ(Jstring)
Non-JSON gets returned as-is.
An alias CJC.fromJSON() is available.
|
Register constructors |
CJC.RegisterConstructors(ArrayOfConstructors)
Example CJC.RegisterConstructors([CmyClass,CmyOtherClass]);
Typically this will appear at the bottom of the source code unit where classes are defined and reference all classes it defines.
|
Additional |
Prevent stringification |
CJC.Block(Class,ClassName)
Sometimes it's essential or just unnecessary to export a sub-element. A good example
of this is a DOM element which is used for placemarking etc while the object is doing its stuff in the browser but
doesn't need to be persisted. If you use jQuery then something like this.placeInDom = $('#contentGoesHere');
is valid and useful, but if you try to export/import you'll hit a big snag. this.placeInDom is actually a jQuery object
which CJC will happily stringify... ... but fail to import into another session as jQuery is now a new object for a new session.
So blocking export is a good thing as it saves a whopping storage bill for something which doesn't need storing anyway.
Example: jQuery is blocked by default but if it wasn't you could use CJC.DoNotStringify(jQuery,'jQuery');
Another reason for blocking export of referenced classes is when you use the following pattern:
Prepare | Get one-off, expensive fiddly stuff out of the way |
Use | Minimum effort for frequent repetition |
In this case you need to 'rebuild from scratch' at the first use then skip the preparation stage on
subsequent calls. If, say, your this.needsInitialising flag is set false at the time
of export then it will be false at import time too so your preparation won't get repeated.
An alias CJC.doNotStringify() is available.
|
Clone a class instance |
Sometimes you need to clone an object and completely disconnect it from any references. Typical
cloning methods copy references inside the object. (Often you want this,
but sometimes you don't.) CJC.DeepCopy(OriginalClassInstance) is a convenience
function to do this. It's really 'To' and 'From' back-to-back with blocks disabled.
|
Internals |
About the code |
CJC.about Version and pointer to this reference page. |
Registered constructors |
CJC.DumpConstructorNames() just for debugging. |
Global |
(In-browser only) CJC_DATA is used internally by CJC and shouln't be hacked. |
Footnote |
In my world functions and methods are always initially capitalised. If this offends you then use the alternatives.
.ToJ() | .FromJ() | .Block |
.toJSON() | .fromJSON() | .doNotStringify() |
|