locked
Namespace define and object with "value" property RRS feed

  • Question

  • I have this code:

    helper = function(data, accessor) {
        var key, keys, result;
        keys = accessor.split('.');
        result = data;
        keys.pop();
        while (keys.length > 0) {
          key = keys.shift();
          if (typeof result[key] !== 'undefined') {
            result = result[key];
          } else {
            result = null;
            break;
          }
        }
        return result;
      };
    
      global = this;
    
      Setting = WinJS.Class.define(function(_id, _defaultValue) {
        var key, map, nameIndex;
        this._id = _id;
        this._defaultValue = _defaultValue;
        if (helper(this, this._id)) {
          throw new Error("Setting " + id + " already defined");
        }
        this._value = this.defaultValue;
        nameIndex = this._id.lastIndexOf(".");
        key = this._id.substring(nameIndex + 1);
        map = {};
        map[key] = this;
        return WinJS.Namespace.define(this._id.substring(0, nameIndex), map);
      }, {
        value: {
          get: function() {
            return this._value;
          },
          set: function(newValue) {
            if (newValue !== this._value) {
              return this.dispatchEvent("change", newValue = this._value, this._value);
            }
          }
        },
        defaultValue: {
          get: function() {
            if (_.isFunction(this._defaultValue)) {
              return this._defaultValue.call(this);
            } else {
              return this._defaultValue;
            }
          }
        }
      });
    
      WinJS.Class.mix(Setting, WinJS.Utilities.eventMixin);
    
      WinJS.Class.mix(Setting, WinJS.Utilities.createEventProperties("change"));
    
      setting = new Setting("ui.settings.decorations", true);
    

    This is supposed to export the whole settings object into the namespace "ui.settings.decorations". However, the base.js initializeProperties has code that checks the members of the exported object for "value" in this line

    if (member.value !== undefined || typeof member.get === 'function' || typeof member.set === 'function') {

    Therefore, not the whole Setting object instance is exported but the "value" that is returned from the getter.

    Is this by design?

    Thursday, December 20, 2012 4:25 PM

Answers

  • Hi Phil,

    I read your initial post again.  You already debugged this and answered the question.

    Yes it is by design.

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 27, 2013 2:29 PM
    Moderator

All replies

  • Anyone? Can this be explained?
    Thursday, March 14, 2013 4:31 PM
  • Can you please explain more about this scenario and what you're expecting to happen, and which part you consider to be a bug?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 22, 2013 6:40 PM
    Moderator
  • My hope was that after calling new Setting("ui.settings.decorations", true); I would be able to use "ui.settings.decorations.value" in the remaining of the JS code. But the property "value" is swallowed by "WinJS.Namespace.define" somehow. If I change the "value" property to "val"everything works as expected:

    ui.settings.decorations.val = true; // calls the setter for the value

    Tuesday, March 26, 2013 4:04 PM
  • Hi Phil,

    value is a reserved keyword used in get and set (excuse syntax errors)

    public string {

    get{

    value = _myVal;

    }

    set{

    _myVal = value;

    }

    }

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, March 26, 2013 7:32 PM
    Moderator
  • I am sorry Jeff, your code makes no sense to me. Is it js or c#?
    Tuesday, March 26, 2013 9:30 PM
  • It's js. Bing " javascript getters and setters" for more information.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, March 27, 2013 12:05 PM
    Moderator
  • Yup it is,

    However, I was THINKING of C# where it is a reserved keyword.   I don't believe that using 'value' is restricted in javascript.  Later on I will try and dive into the namespacedefine function and walk through it in the debugger (or you can Phil) to see if 'value' is used in there and that may be the cause of this.

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 27, 2013 12:09 PM
    Moderator
  • Hi Phil,

    I read your initial post again.  You already debugged this and answered the question.

    Yes it is by design.

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 27, 2013 2:29 PM
    Moderator