angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngModelOption's getterSetter does not work with Factory objects

Open lightswitch05 opened this issue 12 years ago • 15 comments

Overview of the Issue

When using ng-model-options="{ getterSetter: true }", the getter & setter methods do not run within the correct scope if defined in a factory. This causes this to be undefined within the getter & setter method - preventing anything from being set or retrieved. (TypeError: Cannot read property 'x' of undefined)

Motivation for or Use Case

While some getter & setter methods should be defined within the controller & scope - more complex objects should contain their own getter and setter methods defined within their factories for a better separation of concerns.

Angular Version

I've tried this with 1.3.0-rc.4 & 1.3.0-rc.3

Browsers and Operating System

This appears to be Angular.js specific and not a browser problem. However, I am using chrome 37.0.2062.124 m on windows 7 64bit.

Reproduce the Error

Here is a plunker that has getter & setter methods defined within a object from a factory that does not work: http://plnkr.co/edit/RmoegMYEgF9s7s50KQNj?p=preview

Here is another version that has the object defined within the controller (no factory) that works fine: http://plnkr.co/edit/4f8i384nsRwCoaQtPoxx?p=preview

lightswitch05 avatar Oct 02 '14 17:10 lightswitch05

This is kind of a dupe of https://github.com/angular/angular.js/issues/8552 (sort of).

So, the parser isn't the one invoking the function, and we have no way to get the context of the function :(

It would be good to make this work nicely everywhere though

caitp avatar Oct 02 '14 17:10 caitp

I was working with @lightswitch05 on this, and if it's not currently possible to get the context, maybe the docs should be updated to reflect that.

I volunteer @lightswitch05 to update the docs :smile:

jtymes avatar Oct 02 '14 18:10 jtymes

@caitp I propose this as a solution:

getterSetter currently accepts a boolean if getter & setters are to be used. Thats fine - but how about allowing a context to be sent instead of true

thoughts?

lightswitch05 avatar Oct 02 '14 18:10 lightswitch05

I don't think that's particularly useful --- I would rather just change the parser so that it can return the context so that we can get it when we need it.

caitp avatar Oct 02 '14 18:10 caitp

so I don't know how big of a problem this is, but I guess it couldn't hurt to fix it --- I just don't think it's a huge priority.

Maybe I'll write a patch for this on the weekend and see what people think

caitp avatar Oct 02 '14 18:10 caitp

I'm currently working around this by storing a reference to this and using non-prototypical getter & setter methods: http://plnkr.co/edit/nk35FTVECZiC8Uzj8aWY?p=preview

lightswitch05 avatar Oct 02 '14 19:10 lightswitch05

Something like this could work, although I'm not sure how many other things it would break...

In getterFn(path, options, fullExp) (angular.js:11601):

    var code = '';
    forEach(pathKeys, function(key, index) {
      ensureSafeMemberName(key, fullExp);
      code += 'var p = s;\n';
      code += 'if(s == null) return undefined;\n';
      if (index == 0) {
        // but if we are first then we check locals first, and if so read it first
        code += 's=((l&&l.hasOwnProperty("' + key + '"))?l:s)' + '.' + key + ';\n';
      } else {
        // we simply dereference 's' on any .dot notation
        code += 'p = s;\n'
        code += 's=s.' + key + ';\n'
      }
    });
    code += "if (typeof s === 'function' && p != null) return s.bind(p);\n"
    code += 'return s;';

NevilleS avatar Oct 03 '14 18:10 NevilleS

would be a performance killer, not the right way to do this

caitp avatar Oct 03 '14 18:10 caitp

I mean, it is technically the best way to solve the problem, but also would kill performance :(

caitp avatar Oct 03 '14 18:10 caitp

Yeah :smile:

Maybe you could reparse the ngModel expression at call time with a special argument to the lexer...

NevilleS avatar Oct 03 '14 18:10 NevilleS

in my view, it would be easy to provide a separate "getter" function (lets call it context()) which would always return the containing object, or $scope. It would basically just shave off the last chained identifier in an expression.

Obviously this sucks because getting the context (and calling a function using that context) would be the responsibility of the developer, but I don't really see a better way of doing this that doesn't suck the life out of applications.

Alternatively we could just call this behaviour unsupported and never handle it, but I don't think that would make people happy :( WDYT @btford

caitp avatar Oct 03 '14 18:10 caitp

You could do something hacky in NgModelController like:

  var parsedNgModel = $parse($attr.ngModel),
      pendingDebounce = null,
      ctrl = this,
      parsedNgModelParent = null;
  if (ctrl.$options.getterSetter) {
    parsedNgModelParent = parseParent($attr.ngModel);
  }

  var ngModelGet = function ngModelGet() {
    var modelValue = parsedNgModel($scope);
    if (ctrl.$options && ctrl.$options.getterSetter && isFunction(modelValue)) {
      modelValue = modelValue.call(parsedNgModelParent);
    }
    return modelValue;
  };

That way you can still have the nice benefit of the lexer providing the context (not the developer), but you limit the performance impact to a second, optional call to $parse.

Still pretty gross :unamused:

NevilleS avatar Oct 03 '14 19:10 NevilleS

I think this reasonably should be supported.

btford avatar Oct 03 '14 22:10 btford

I have a scenario when this really is a blocker - when you use TypeScript. I use TypeScript to implement all of my application logic including controllers, models etc. TypeScript generates methods using prototype. None of getters/setters then work with AngularJS due to this issue.

When you mention that it would be a performance killer, have you actually tried how much?

I made a small test: http://plnkr.co/ABCvAb6FrYO2PNB7J7ic

Approach with bind() is slower as we expect. In IE11 it's about two times slower than current approach. In Chrome it's about 10 times slower. The test just runs each version one million times in a loop so it's kind of artificial but shows the difference. For that 1 million calls it makes about 1 second in Chrome.

Now the question is, if you run full AngularJS processing for single getter, what is the relative slowdown? If whole processing executed 1000 times today takes let's say 1 second and with this change would take 1.1 second, I would go for it. Today you generate code for the getter as a string and let runtime parse it and execute it. That sounds like a much bigger performance hit than calling bind() (though I haven't tested that).

Current behavior that prevents you to use object methods correctly makes getter/setter option kind of useless.

Katulus avatar Oct 24 '14 17:10 Katulus

Hmm. Something like this would be more explicit, and let you specify the caller for your getter function if you wanted...

  var parsedNgModel = $parse($attr.ngModel),
      parsedNgModelContext = $parse($attr.ngModelContext),
      pendingDebounce = null,
      ctrl = this;

  var ngModelGet = function ngModelGet() {
    var modelValue = parsedNgModel($scope);
    if (ctrl.$options && ctrl.$options.getterSetter && isFunction(modelValue)) {
      var context = parsedNgModelContext($scope) || this;
      modelValue = modelValue.call(context);
    }
    return modelValue;
  };

  var ngModelSet = function ngModelSet(newValue) {
    var getterSetter;
    if (ctrl.$options && ctrl.$options.getterSetter &&
        isFunction(getterSetter = parsedNgModel($scope))) {
      var context = parsedNgModelContext($scope) || this;
      getterSetter.call(context, ctrl.$modelValue);
    } else {
      parsedNgModel.assign($scope, ctrl.$modelValue);
    }
  };

That would make something like this work as you were hoping, where this is bound to your factory instance when the getter/setter method is called:

<input ng-model="someFactory.getterSetter"
       ng-model-options="{ getterSetter: true }"
       ng-model-context="someFactory">

I think that's what @caitp was originally proposing, and this doesn't require touching $parse at all.

NevilleS avatar Oct 24 '14 22:10 NevilleS