Friday 29 May 2015

Pull to refresh

The idea is to load in new items to the top of a feed by pulling down from the top of the list until you see a loading indicator, letting go, and watching as new items magically (not really) add themselves in.

 Ionic has an awesome directive that has undergone a redo fairly recently to accomplish exactly this.


ionRefresher (Pull to Refresh Directive)

The Ionic directive we are going to use is the ion-refresher

 The most basic usage is as follows:


<ion-refresher on-refresh="doRefresh()"></ion-refresher>

 on-refresh should point to a $scope function that gets the new data, updates a list, and then lets the refresher know it is done. This refresher should be above some kind of list.


 Example


<ion-refresher on-refresh="doRefresh()"></ion-refresher>
<ion-list>
  <ion-item class="item-avatar" ng-repeat="item in items">
    <img src="{{item.user.picture.thumbnail}} " />
    ## {{item.user.name.first}} {{item.user.name.last}}
    <p>{{item.user.location.city}} {{item.user.password}}</p>
  </ion-item>
</ion-list>

 

Factory

In our example, we’re going to be making a call to the Random User API to get some data to play with. To do this, we’ll create a factory that makes these API calls. This factory will have two methods: GetFeed and GetNewUser. GetFeed will be called when our app loads to get the initial data, and the GetNewUser will be called each time we do a pull to refresh.


.factory('PersonService', function($http){
  var BASE_URL = "http://api.randomuser.me/";
  var items = [];

  return {
    GetFeed: function(){
      return $http.get(BASE_URL+'?results=10').then(function(response){
        items = response.data.results;
        return items;
      });
    },
    GetNewUser: function(){
      return $http.get(BASE_URL).then(function(response){
        items = response.data.results;
        return items;
      });
    }
  }
})


Controller

Our controller needs to do 2 things:
  1. Fill the feed with the initial items
  2. Handle the pull to refresh
First, to fill our feed, we’ll want to make a call to the PersonService and assign the result to the $scope.items array.
Next, we need to handle the pull to refresh. Recall we configured our directive to call a doRefresh function.
In this function, we should call the GetNewUser function and add these items to the beginning of the array.
.controller('MyCtrl', function($scope, $timeout, PersonService) {
  $scope.items = [];

  PersonService.GetFeed().then(function(items){
    $scope.items = items;
  });

  $scope.doRefresh = function() {
    PersonService.GetNewUser().then(function(items){
      $scope.items = items.concat($scope.items);

      //Stop the ion-refresher from spinning
      $scope.$broadcast('scroll.refreshComplete');
    });
  };

});


 

Friday 22 May 2015

To resolve EACESS issue, when installing yo

Install an AngularJS generator

You can install Yeoman generators using the npm command.
Install prerequisites
Before installing Yeoman, you will need the following:
  • Node.js v0.10.x+
  • npm (which comes bundled with Node) v2.1.0+
  • git
You can check if you have Node and npm installed by typing:
 node --version && npm--version
You can check if you have Git installed by typing:
  • git --version
Install the Yeoman toolset
Once you’ve got Node installed, install the Yeoman toolset:
  • npm install --global yo bower grunt-cli
 Errors?
If you see permission or access errors, such as  EACCESS, do not use sudo as a work-around.Then
  • sudo chown -R $USER:$GROUP ~/.npm
  • npm cache clean  
then install yo
  •  npm install --global yo bower grunt-cli 

Confirm installation

  • yo --version && bower --version && grunt --version

Install an AngularJS generator

  • npm install --global generator-angular@0.11.1

Friday 15 May 2015

ngNotify in angularjs

ng-notify:-

 A simple, lightweight module for displaying notifications in your AngularJS app.


Implementation:-

 

a)Requirements

AngularJS is the only dependency. Animation is achieved with pure JS, jQuery not necessary.


b)Installation

You can install ng-notify with Bower.


 bower install ng-notify --save



c)Usage

After including ng-notify.min.js and ng-notify.min.css, inject the ng-notify provider into your project.

var app = angular.module('demo', ['ngNotify']);
 
Now you can trigger notifications from anywhere in your app. To display a notification, just use the set method.

ngNotify.set('Your notification message goes here!');
 
To specify the type of notification to display, provide the optional type param. (For available types, check the definitions below.)
ngNotify.set('Your error message goes here!', 'error');
 
 
 

Advanced Usage:-

a)Default Configuration

You can override the default options for all notifications by using the config method. None of these options are required. (For available options, check the definitions below.)
ngNotify.config({
    theme: 'pure',
    position: 'bottom',
    duration: 3000,
    type: 'info',
    sticky: false
});


b)Individual Configurations

You can also pass an object of options to individual notifications. You can pass through any combination of our available options here as well. (For available options, check the definitions below.) For example:

 ngNotify.set('Your first message.', {
    position: 'top',
    sticky: true
});

ngNotify.set('Your second message.', {
    type: 'error',
    duration: 2000
});

ngNotify.set('Your third message.', 'error'); // Original use case still works, too.

ngNotify.set('Your fourth message.', {
    theme: 'pitchy'
});


Friday 8 May 2015

ionic inputs

Text

First up is the Text Input, the most common input type used. The code is pretty standard, and if you’re used to web development this input isn’t anything new. This is also the fallback if a device does not support a certain HTML input type.


 <label class="item item-input">
  <span class="input-label">Username</span>
  <input type="text">
</label>


Email

The Email Input should be used anytime you require a user to enter an email address. It’s important to take advantage of this input type because it offers a much smoother keyboard experience, displaying the @ and . characters within the initial keypad for quicker typing.


 <label class="item item-input">
  <span class="input-label">Email</span>
  <input type="email">
</label>



Tel

The Tel Input is a very unknown and underused input type. It displays a full numeric keypad, which makes for a great user experience when all that is required from the user is numbers. Take advantage of this one!


 <label class="item item-input">
  <span class="input-label">Telephone</span>
  <input type="tel">
</label>


Search

The Search Input doesn’t do anything too different from the text input except change the standard “go” button to read “search” instead.


<label class="item item-input">
  <i class="icon ion-search placeholder-icon"></i>
  <input type="search" placeholder="Search">
</label>



Number

The Number Input is another way to offer numbers for your users. The difference between the number input and the tel input is that also offers easy access to special characters and symbols. However, if you only need numbers entered, it’s best to stick with the tel input.



<label class="item item-input">
  <span class="input-label">Number</span>
  <input type="number">
</label>



Date

The Date Input should be utilized for when a user is asked to enter the month, day, and year. The mobile keyboard will offer a nice interactive panel for easy entering.


<label class="item item-input">
  <span class="input-label">Date</span>
  <input type="date">
</label>


Month

The Month Input will trigger a keyboard experience similar to the date input, however it leaves out the “day” option. Use this input when your user only needs to enter the month and year. A perfect example would be for taking credit card expiration dates.


 <label class="item item-input">
  <span class="input-label">Month</span>
  <input type="month">
</label>


Password

The Password Input is another commonly used input in normal web development. It hides the characters so nobody else can see what the user is typing.



 <label class="item item-input">
  <span class="input-label">Password</span>
  <input type="password">
</label>


Friday 1 May 2015

http request

What is HTTP?

The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.
HTTP works as a request-response protocol between a client and server.
A web browser may be the client, and an application on a computer that hosts a web site may be the server.
Example: A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content.



Two HTTP Request Methods: GET and POST

Two commonly used methods for a request-response between a client and server are: GET and POST.
  • GET - Requests data from a specified resource
  • POST - Submits data to be processed to a specified resource

 Simple GET request example :


$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

 Simple POST request example (passing data) :


$http.post('/someUrl', {msg:'hello word!'}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });



In addition, you can supply a headers property in the config object passed when calling $http(config), which overrides the defaults without changing them globally.

To explicitly remove a header automatically added by $httpProvider.defaults.headers on a per request basis, Use the headers property, setting the desired header to undefined. For example:

 var req = {
 method: 'POST',
 url: 'http://example.com',
 headers: {
   'Content-Type': undefined
 },
 data: { test: 'test' }
}

$http(req).success(function(){...})
      .error(function(){...});