Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: puremvc.declare use  (Read 6062 times)
saad
Sr. Member
****
Posts: 65


View Profile Email
« on: April 19, 2012, 12:18:18 »

Any example please.

puremvc.declare("com.Test", { a: 4 });

//use
alert(com.Test.a);

please highlight on the scope param.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: April 19, 2012, 10:50:09 »

From the documentation for declare: http://puremvc.org/pages/docs/JS/native-multicore/#!/api/puremvc

Declare a namespace and optionally make an Object the referent of that namespace.

:
console.assert(null == window.tld, 'No tld namespace');
// declare the tld namespace
puremvc.declare('tld');
console.assert('object' === typeof tld, 'The tld namespace was declared');

// the method returns a reference to last namespace node in a created hierarchy
var reference= puremvc.declare('tld.domain.app');
console.assert(reference === tld.domain.app)

// of course you can also declare your own objects as well
var AppConstants=
    {
           APP_NAME: 'tld.domain.app.App'
    };

puremvc.declare('tld.domain.app.AppConstants', AppConstants);
console.assert(AppConstants === tld.domain.app.AppConstants,
                    'AppConstants was exported to the namespace');

Note that you can also declare within a closure. That way you can selectively export Objects to your own namespaces without leaking variables into the global scope.

:
(function(){
    // this var is not accessible outside of this
    // closures call scope
    var hiddenValue= 'defaultValue';

    // export an object that references the hidden
    // variable and which can mutate it
    puremvc.declare
    (
         'tld.domain.app.backdoor'

    ,    {
             setValue: function (value)
             {
                 // assigns to the hidden var
                 hiddenValue= value;
             }

    ,        getValue: function ()
             {
                 // reads from the hidden var
                 return hiddenValue;
             }
         }
    );
})();
// indirectly retrieve the hidden variables value
console.assert('defaultValue' === tld.domain.app.backdoor.getValue());
// indirectly set the hidden variables value
tld.domain.app.backdoor.setValue('newValue');
// the hidden var was mutated
console.assert('newValue' === tld.domain.app.backdoor.getValue());

On occasion, primarily during testing, you may want to use declare, but not have the global object be the namespace root. In these cases you can supply the optional third scope argument.

:
var localScope= {}
,   object= {}

puremvc.declare('mock.object', object, localScope);

console.assert(null == window.mock, 'mock namespace is not in global scope');
console.assert(object === localScope.mock.object, 'mock.object is available in localScope');

Hope this helps,
-=Cliff>
Logged
saad
Sr. Member
****
Posts: 65


View Profile Email
« Reply #2 on: April 22, 2012, 06:56:01 »

Thanks, the latest build solved the problem as well.
Logged
Pages: [1]
Print