Prefetching templates in your ionic framework app easily

Prefetching and template caching is one of the ways to improve your angularjs application and so it is recommended for your ionicframework app.

Here I’m using angular ui router for the routes and looks like

$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})

.state('app.news', {
url: "/news/:category",
views: {
'menuContent': {
templateUrl: "templates/news/listing.html",
controller: 'NewsListsCtrl'
}
}
})

.state('app.news.detail', {
url: "/detail/:id",
views: {
'menuContent@app': {
templateUrl: "templates/news/detail.html",
controller: 'NewsDetailCtrl'
}
},
resolve: {
newsDetail: function($stateParams, $q, myservice) {

//...

}
}
})
...

Normally we would use list all the templates manually and then prefetch

var templates = [
"page.html",
"page2.html"
];

But in angular ui router we can use

$state.get()

to get the list of all the states. We can then extract the template names from there

In my hybrid app based on ionicframework I used following code to prefetch all my templates easily.

 

...
.run(['$http', '$templateCache','$state',
function($http, $templateCache,$state) {
/**
* Template prefetching
*
*/
var templates = findNested($state.get(),'templateUrl');
        var promises = [];

    templates.forEach(function (c,i) {
        if ($templateCache.get(c)) return; //prevent the prefetching if the template is already in the cache
            promises[i] = $http.get(c).success(function (t) {
              $templateCache.put(c, t);
              console.log(c+ 'loaded')
            });
        });
     });
}
console.timeEnd('templatesPrefetch');
}
])

findNested is a function with loadash as dependency

function findNested(obj, key, memo) {
_.isArray(memo) || (memo = []);
_.forOwn(obj, function(val, i) {
if (i === key) {
memo.push(val);
} else {
findNested(val, key, memo);
}
});
return memo;
}

Note you need to include lodash as dependency for the above function

PHP debugging through email

As a developer we know that debugging can be very handy sometimes. So it is good to know about different debugging tools and methods. Of course, all of them do not work out for every situation so I think it is good to learn about new ways of debugging!.

At the time of writing this blog post, its been more than 4 years since I started programming in PHP. Right now I’m having mixed feeling; feeling of shamefulness that I don’t know many things yet and also feeling of excitement that I’m learning new things everyday!
I want to share one very important tool in PHP that can be very important in debugging- error_log. Yes that’s right its about error_log. I’m damn sure almost everyone who is reading this knows about this (if you are or ever been into PHP programming).
But I’m here to let you know about another very helpful feature of this function which is capable of sending emails about the error

Here is the syntax according to PHP manual
bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
An example use

error_log("testing".$debugString, 1,"developer@yourdomain.com","From: debugger@yourdomain.com\nSubject: Debugging this value");

I found this pretty helpful in debugging in live environment where you want to keep track of different values while still keeping things clean.

Happy debugging!

HTTP Header alternative for CURLOPT_USERPWD

Today I had a situation where i needed to quickly make a test http request to test a service that required http authentication which was made using php curl in the code. I prefer to use Postman chrome extension In curl the authentication was made like

curl_setopt($ch, CURLOPT_USERPWD, "$application_key:$application_master_secret");

After doing a small research I found that’s the Authorization header.

So for Basic method, It would be base64 encoded value of “username:password” prepended with “Basic”. For eg,

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

In my case it would be
“Authorization: Basic ” . base64_encode($application_key. “:” . $application_master_secret)