ternjs / tern

A JavaScript code analyzer for deep, cross-editor language support
https://ternjs.net/
MIT License
4.25k stars 378 forks source link

AngularJS doesn't inject custom service. #267

Closed angelozerr closed 10 years ago

angelozerr commented 10 years ago

With AngularJS you can define a 'cart' service of module like this :

var foodMeApp = angular.module('foodMeApp');
foodMeApp.service('cart', function Cart(localStorage, customer, $rootScope, $http,     alert) {
    var self = this;
    self.total = function() {
    }
});

And you can inject the 'cart' service in a controller like this :

 foodMeApp.controller('CheckoutController', function CheckoutController($scope, cart) {
      cart. // <- here completion for total method
     };
 });

It should be cool if tern could manage the completion for cart properties.

I have started to studied how to manag ethat, but no success for the moment. I think applyWithInjection should be improved to inject the 'cart' service. I try to create an instance of cart like this :

 var result = new infer.AVal;    
 fnType.propagate(new infer.IsCtor(result, null));
 fnType.propagate(new infer.IsCallee(infer.cx().topScope, deps, null, result));    

Many thank's for your help.

angelozerr commented 10 years ago

Marijn could you give me some directives to manage that please when you will have time. Thank's!

marijnh commented 10 years ago

Well, the crappy docs 1 just spew a bunch of jargon, and don't really get into details, but the way I thought service worked (and the way the plugin implements it) is that it is supposed to return a value, and that is the value that will be injected.

If it is even more magical than that, please explain how it works.

marijnh commented 10 years ago

Okay, it appears that service calls new on the function it gets. Attached patch should help.

angelozerr commented 10 years ago

Wow, many thank's, it works like a charm.