2

I have a 3rdparty.JS file at app\scripts\vendor\3rdparty.JS

angular.module('clientApp').factory('myFactory', function () {

        // HOW can I get the 3rdparty.JS file here in form of an object?

        // Public API here
        return {

        };
    });

How can I now access the 3rdparty library in form of an object to access its methods?

HelloWorld
  • 4,671
  • 12
  • 46
  • 78
  • What does the library do? Couldn't you simply wrap it into an Angular service? – JB Nizet Mar 21 '14 at 22:24
  • Its Linq.JS library providing an "Enumerable" object when I have used it with requireJS and knockoutJS. – HelloWorld Mar 21 '14 at 22:26
  • not familiar with Linq.JS does it provide objects that you create. Typically you will just load a library using a script tag as per usual then you just instantiate the object where you need it within Angular (in a service or directive to abstract it) – shaunhusain Mar 21 '14 at 22:27
  • you mean there exist no dependency injection with 3rdparty libs? – HelloWorld Mar 21 '14 at 22:30
  • You can use `value()` to do that... `angular.module('clientApp').value('Enumerable', Enumerable);` Then you can inject it into your service... `angular.module('clientApp').factory('myFactory', function (Enumerable) { ... ` – Anthony Chu Mar 21 '14 at 22:30
  • Does value function take an absolute path or any path at all? I can not just write there linq.js I guess? ;P – HelloWorld Mar 21 '14 at 22:32
  • @HelloWorld: it seems you're confusing loading a JS file (that is done in the HTML, using ``, whatever the JS file is or contains), with injecting a JavaScript object in another object. – JB Nizet Mar 21 '14 at 22:34
  • You would still have to include the script yourself (or use something like requirejs to bring it in). – Anthony Chu Mar 21 '14 at 22:34
  • So with angulars DI mechanism I can not include/inject an extern .JS file like I can do with requireJS ? – HelloWorld Mar 21 '14 at 22:40
  • Angular is not an AMD. edit, you can combine them: http://stackoverflow.com/questions/12529083/does-it-make-sense-to-use-require-js-with-angular-js – Jorg Mar 21 '14 at 23:07
  • I am using angularAMD now, lets see how it works out :) – HelloWorld Mar 21 '14 at 23:16

1 Answers1

0

Me too like using linq in angular apps. Those linq operations are time savers. Fortunately we have a plain javascript library equivalent to this.

linq.js in http://linqjs.codeplex.com/ is awesome and stable.

If someone can port this to bower package in github. That would be much helpful.

However, I tried this below and worked for me,

Full Content in the Plunker - http://plnkr.co/edit/Zy1W3G?p=info

// Add service to your module
//linq.svc.js

(function() {
  'use strict';

  angular
    .module('mymodule')
    .factory('$linq', linqService);

  function linqService() {

    return {
      getEnumerable: Enumerable
    };

    function Enumerable() {
      var Enumerable = function(getEnumerator) {
        this.GetEnumerator = getEnumerator;
      }

      ...... < Paste the remaining content here >
        ......
        ......

      // out to global
      return Enumerable;
    }
  }


})();

//Example Usage:
//mycontroller.js

(function() {
  'use strict';

  angular.module('mymodule')
    .controller('mycontroller', MyController);

  MyController.$inject = [
    '$linq'
  ];

  function MyController(
    $linq
  ) {


    var jsonArray = [{
      "user": {
        "id": 100,
        "screen_name": "d_linq"
      },
      "text": "to objects"
    }, {
      "user": {
        "id": 130,
        "screen_name": "c_bill"
      },
      "text": "g"
    }, {
      "user": {
        "id": 155,
        "screen_name": "b_mskk"
      },
      "text": "kabushiki kaisha"
    }, {
      "user": {
        "id": 301,
        "screen_name": "a_xbox"
      },
      "text": "halo reach"
    }]

    var queryResult = $linq.getEnumerable().From(jsonArray)
      .Where(function(x) {
        return x.user.id < 200
      })
      .OrderBy(function(x) {
        return x.user.screen_name
      })
      .Select(function(x) {
        return x.user.screen_name + ':' + x.text
      })
      .ToArray();

    console.log("queryResult==>", queryResult);

  }
})();

Example Output:

queryResult==> ["b_mskk:kabushiki kaisha", "c_bill:g", "d_linq:to objects"]
Rajkumar
  • 1
  • 4