0

Redirect to the original requested page after login using AngularJs

What we want to do is that when the user tries to go to a page and he is not logged in, we want to redirect him back to that page after he login successfully There is two different scenarios that we want to handle

  • The user is not logged in to our site and tries to access it (no cookie stored)
  • When the user is already in our site and he makes a request to the server and the the server  returns unauthorized (401) (in case the user is logged in and the cookie times out)

Continue reading

0

AngularJs Scroll to element using directives

I want to implement a simple concept using angular, scrolling to a specific area of the page using a directive, I found myself needing this in a pagination example, I want to scroll to the top of the list when the user clicks on next page

Whenever I am trying to implement a directive like that I always try to first write how I would want to represent it in the html.

Continue reading

11

How to do Authorization and Role based permissions in AngularJs

We will be adding 3 Layer to implement Authorization in our app

  • UI manipulation (Showing or hiding part of the screens based on the user permission)
  • Routing ( When the user access a route that he doesn’t have permission to will redirect him to an error  route )
  • Http Interceptor (We will Setup an interceptor that will check when the server returns 401 or 403  and will redirect the user to an error route)

Continue reading

4

AngularJs Good Unit Test Structure For Controllers & How to test ajax code and Promises

The poorly the structure is made for unit testing the more and more complected it will take to write a unit test for a simple functioning piece of code , and it will feel that your fighting your way against testing, instead it should feel more fun! So i’ll be showing a structure that worked for me very well when working with angular controllers Before going deep with the structure, i’ll be using

  • Jasmine (unit testing)
  • Jasmine spies (mocking)
  • karma (because its awesome)

Continue reading

28

Build AngularJS Grid with server side paging, sorting & filtering

I will be showing how to build a grid that supports server side paging, sorting & filtering

My goal with this  is that i didn’t want to implement a more complex approach that will be hard to customize later, so what i ended up doing is implementing a solution that will show a list of data in a table like structure and implement a reusable solution for paging, sorting & filtering

Continue reading

0

Date parsing with JavaScript, Introducing Moment.js

JavaScript already has a Date.parse() method, why would we talk about date parsing?

Because in short the Date.parse() method implementation is dependent on the browser itself, so it could return a different result in chrome other than Firefox like here

Introducing Moment.js

Moment.js is a library for parsing, validating, manipulating, and formatting dates.

it has an excellent documentation, it has plenty of helpful examples

Parsing & Validating dates

var isValid = <code>moment("12-252-1995").isValid() //returns false
var date = moment("12-25-1995", "MM-DD-YYYY") //prase a date using a specify format, if the actual date is in a different format isValid() will return false
var date = moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]) //parse a date with a list of possible formats that it could matches

//using it as a wrapper of a date object
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);

Bonus for Asp.net MVC users

ASP.NET returns dates in Json as /Date(1198908717056)/ or /Date(1198908717056-0700)/

This will happen often if you try to make an ajax call to an asp.net action and it returns a JsonResult that contains DateTime

var date = moment("/Date(1198908717056-0700)/") // December 28 2007 10:11 PM