0.1.0 • Published 9 years ago

angular-get v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

What Does It Do?

This package centralizes the grunt work of setting up dynamic GET requests for data in Angular applications. Simply require the angular-get package and inject it into your main angular module. Then in your factories inject AppGet.

How to Use in Angular

npm install angular-get --save
var AppGet  = require('angular-get');
var angular = require('angular');


//This is your main angular module.
angular.module('YourApp', ['AppGet']) //Inject AppGet angular module.

//This is your factory, dependancy inject the AppGet Factory.
.factory('TestFactory', ['AppGet', function(AppGet){
    var TestFactory = {};
   TestFactory.get = AppGet.get('http://jsonplaceholder.typicode.com/comments?postId=[d]');

    TestFactory.get([1]).then(function(response){
         console.log(response); // [obj, obj, obj]
    })

    return TestFactory;
}]);

INITIALIZING A FACTORY TO USE AppGet

TestFactory.get = AppGet.get('http://jsonplaceholder.typicode.com/comments?postId=[d]');

WHAT IS REALLY HAPPENING

  • d is the dynamic parameter property, you can have many or as few or none.
  • If you have a dynamic paramter, it > MUST < be "d" in the assigning string.
  • When TestFactory.get(array of params) is run, it will iterate over the array of params and inject them sequentially into the string prior to sending the request to the server.

RUNNING THE GET METHOD IN YOUR APP

TestFactory.get = AppGet.get('http://jsonplaceholder.typicode.com/comments?postId=[d]');

IMPORTANT get(Array) accepts an array of params

  • Do not send in {} Objects, Strings, or arguments. ONLY arrays if params exist. TestFactory.get(["array"]) // good

  • If you have no params, do not include an empety array, leave it as get(). TestFactory.get()

  • if you have no params, your assigning string should not have a d in it 'http://...com/comments?postId=[d]' <-- dynamic param

  • Once the method runs, it will inject all the params into the request string 'http://...com/comments?postId=1' <-- once request is being called

RETRIEVING THE DATA

  • All GET methods in AppGet return back a promise. To retrieve the data, use the .then() funciton.
TestFactory.get([1]).then(function(response){
    console.log(response); // [obj, obj, obj]
    });

Example TestFactory > > ./test-factory.js

  • Check the npm_modules folder for the test-factory.js file. You can use this as base template and also test the AppGet module in your app.