topleft topright

Welcome, Guest. Please login or register.
May 23, 2013, 10:48:25 AM
Home Help Search Login Register
News: ATTENTION: Spambots must die! Humans must visit http://contact.futurescale.com to request forum access.

Pages: [1]
Print
Author Topic: puremvc.declare use  (Read 715 times)
saad
Full Member
***
Posts: 21


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

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: 2790



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

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.

Code:
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.

Code:
(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.

Code:
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
Full Member
***
Posts: 21


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

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



Login with username, password and session length

Powered by SMF 1.1.11 | SMF © 2006-2007, Simple Machines LLC
Copyright © 2006-2008 Futurescale, Inc.