# Code coverage report for lib/accessors/bindableObject.js

## Statements: 68.97% (20 / 29) Branches: 62.5% (15 / 24) Functions: 75% (9 / 12) Lines: 78.26% (18 / 23) Ignored: none

All files » lib/accessors/ » bindableObject.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    661

    1

    1

    299
    219
    219

    345

    100

    24

    24
    24
    24
    24

    226

    18
    5

    13

    18

     var BaseAccessor   = require("./base"),
    BindableOject      = require("bindable-object"),
    BindableCollection = require("bindable-collection");

    function BindableObjectAccessor () {

    }

    module.exports = BaseAccessor.extend(BindableObjectAccessor, {
      accessible: function(context) {
        return context && context.__isBindableObject;
      },
      castObject: function(context) {
        if (context.__isBindable) return context;
        Iif (context.toString() === "[object Array]") return new BindableCollection(context);
        return new BindableOject(context);
      },
      castCollection: function(context) {
        if (context.__isBindable) return context;
        if (context.toString() === "[object Array]") return new BindableCollection(context);
        return new BindableOject(context);
      },
      get: function(context, path) {
        return context.get(path);
      },
      set: function(context, path, value) {
        return context.set(path, value);
      },
      call: function(context, path, params) {

        var fnPath = path.pop(),
        ctxPath    = path.length ? path : void 0;

        // TODO - check for ctxPath undefined
        var ctx = ctxPath ? context.get(ctxPath) : context;
        Iif (!ctx) return;
        var fn = ctx[fnPath];
        Eif (fn) return fn.apply(ctx, params);
      },
      watchProperty: function(context, path, listener) {
        return context.watch(path, listener);
      },
      watchEvent: function(context, event, value) {

        if (event === "change" && context.__isBindableCollection) {
          return context.on(event, value);
        }

        return {
          dispose: function(){}
        };
      },

      /**
       * TODO - deserialize is improper. Maybe use something like
       * normalize, toArray
       */

      normalizeCollection: function(collection) {
        return collection.source || collection;
      },
      normalizeObject: function(object) {
        return object.toJSON();
      }
    });