background preloader

Package.json: an interactive guide

Package.json: an interactive guide

JSON JSON (JavaScript Object Notation) is a data-interchange format. It closely resembles a subset of JavaScript syntax, although it is not a strict subset. (See JSON in the JavaScript Reference for full details.) JSON is capable of representing numbers, booleans, strings, null, and arrays (ordered sequences of values) and objects (string-value mappings) composed of these values (or of other arrays and objects). Using JSON through JavaScript's JSON object The simplest, easiest way to use JSON is through the standard JSON object in JavaScript. For example, to serialize a JavaScript object, use the JSON.stringify() method: var foo = {}; foo.bar = "new property"; foo.baz = 3; var JSONfoo = JSON.stringify(foo); var backToJS = JSON.parse(JSONfoo); For more information, see the JSON object documentation. toJSON() method If an object defines a toJSON() method, JSON.stringify will call that method to determine the object's JSON representation. json1 now contains '"bar"'. Limitations See also nsIJSON

Pass Values From the Page to AngularJs - Jake Trent There are simple mechanisms for passing data from your web page to AngularJs. One directive is called ngInit. You may well need it. The Use Cases The use cases don’t make a ton of sense without a concrete example. There are several ways to implement even the most simple example. For our purposes, let’s say that we’re displaying a list of comments on the screen. Angular Requests Data It’s easy to put data into scope for AngularJs to use if Angular is the one to request the data initially. Then your template will automatically have the data in scope. Now when we add a new comment via Ajax, we can push it right into the comment collection, and the page is updated by Angular with the appropriate data. Server Renders Data Initially Sometimes it’ll make sense for the server to render the data. If the server was to initially print the comments, like so: We still might want to do AJAX’y things with a comment, such as like it. ngInit Passes Angular Server Data From the ngInit docs:

Make Jsonp Requests with AngularJs - Jake Trent There are a couple ways to make cross domain requests from a client browser. One method is called Jsonp or Json with padding. The AngularJs library makes this really simple. Web Server Support First of all, your Jsonp request will be worth nothing if the server you’re requesting data from does not support it. But that’s kind of a weird name. This essentially is a browser hack to get around the security constraint of browsers not being able to request non-script items from different domains. Angular Jsonp You’re probably already used to using the $http service. The client script also requires that we specify the callback to send data to. All that jibber jabber for this simple example of a script asking for Tumblr data: The interesting line is line one.

Preload Route Data in Angular - Jake Trent It’s sometimes useful to be able to preload or resolve data previous to an Angular view being displayed. Such was the case recently for our current user object. It simplified a lot of things in the app to have the guarantee of a loaded current user previous to displaying pages. The Angular router has a great mechanism built in to do just this sort of thing, called resolve. Current User For the example, our current user object is super simple. Note that for our app there is just one CurrentUser object, a singleton, because we return a single instance from our factory, not a class. Router The Angular router is used for client-side routing of urls. Let’s say that we have a home page and a profile page for our example. Each route can be configured with a resolve object. The run method is essentially the Angular app’s main method, starting the program. Resolve Function In our resolve function we setup our own promise via the $q service. Application Controller When to Use

Custom Sorting in AngularJS - Jaco Pretorius Today I had to implement custom sorting in AngularJS. As with most things in Angular it’s very simple once you figure out how. AngularJS has an orderBy function which can be used to sort a list of objects by any property. Sort by a Property As an example, let’s create a list of contacts. I can now easily display these contacts in a table by using the ng-repeat directive. Now let’s add a select with 2 different sort options. Because we are using ng-model here we also need to specify a default sort order in our controller. Now we simply need to specify the orderBy clause in the ng-repeat directive. You can see the full example in this jsFiddle. Sort with a Custom Function The documentation also says that we can sort using a function instead of a simply property. I’m going to use the same HTML and contacts as before, but I’m going to change the ng-repeat directive to the following: Now I’m going to implement a function called randomSort which will be used to sort the objects.

Hacking AngularJS This post comes from Angel Todorov at the Infragistics blog. In this blog post, I would like to cover two things: How I've extended AngularJS in order to support transaction logs (detailed diffs) for arrays. I've forked the project on github: How I've created a custom Angular directive for the Ignite UI grid. As a result, you will see how the IgniteUI Grid integrates very nicely with Angular, and also supports full two-way databinding. The attached project contains everything you need to see it in action for yourself, just unzip and run angular.html. Let's start with some background information on the way AngularJS performs updates between models and views. It's a very elegant approach, a lot more elegant than changing listeners. 01.scope. 02. if (Array.isArray(diff)) { 03. for (var i = 0; i < diff.length; i++) { 05. if (! 06. continue; 08. for (var j = 0; j < diff[i].txlog.length; j++) { 1.

rb: Displaying json array data with Angular JS and ng-grid In many projects there comes a time when you’ll need to list some data. It may be a requirement or just an improvement for your users to show the list of products, but you have to decide which of the available options you will use in your application. This article will help you choose the best one, for your app. In past, i’ve used tables or unordered list a lot bit in the past. So manipulating the data can be tough. Thankfully there are some libraries available and that have built in functionality for sorting, searching, grouping data, etc. I have tried with Ng-grid which is basically a Angular Data Grid written in AngularJS and jQuery by the AngularUI Team AngularJS is a client-side JavaScript framework by Google that makes writing sophisticated, interactive Web apps super easy. AngularUI - The companion suites to the AngularJs framework The first step is to create a blank HTML file and fill it with the following: Now apply the basic styles to your grid Basic Json schema looks like this

TutorialCachingStory - memcached - This is a story of Caching - Memcached ed note: this is an overview of basic memcached use case, and how memcached clients work Two plucky adventurers, Programmer and Sysadmin, set out on a journey. Together they make websites. One day the Sysadmin realizes that their database is sick! Our plucky Sysadmin eyes his webservers, of which he has six. "So now what?" Our adventurous Programmer grabs the pecl/memcache client library manual, which the plucky Sysadmin has helpfully installed on all SIX webservers. $MEMCACHE_SERVERS = array( "10.1.1.1", //web1 "10.1.1.2", //web2 "10.1.1.3", //web3); Then he makes an object, which he cleverly calls '$memcache'. $memcache = new Memcache();foreach($MEMCACHE_SERVERS as $server){ $memcache->addServer ( $server );} Now Programmer thinks. Are the results of this select in memcache? // use $huge_data_for_front_page how you please Programmer pushes code. Hey, 'get huge_data_for_front_page' are you there? The first memcached does not answer... The second memcached does not answer...

Awkward Change Flow or: How I Learned to Stop Worrying and Love the Application Cache - A Modest Proposal Snappy title, yes...? Let's just move on... The application cache API introduced by HTML5 is a very powerful tool which is particularly useful for web applications whose intended primary use is on mobile devices. Not only can it significantly speed up the load time of your app, but if implemented correctly you can make your application work quite well even when the user is not connected to the Internet. These articles describe the API really well, and also notes several of the "gotchas" that might trip you up if you're not aware of them. One of the more annoying quirks of developing with the application cache is that it has no mechanism for detecting updates to already cached resources. So, we need a way of automatically updating this comment in the manifest whenever a change has been detected in one of our cached files! To install all the necessary packages, just run the following commands in your project folder (assuming you already have installed npm):

Databinding in angularjs Advanced Testing and Debugging in AngularJS Learn to test your AngularJS application like a Pro using the latest and greatest AngularJS is becoming immensely popular and mainstream which means that there is a lot of AngularJS code out there that is being tested or is yet to be tested. And now that you're well on your way to test like a pro, thanks to the abundance of articles, tutorials, books and material out there on AngularJS testing & development, testing should be a mandatory process of your web development workflow. Full-Spectrum testing with AngularJS & Karma taught us how to test certain areas of your AngularJS application, but how do we test efficiently? How do we debug a problem down the root cause? How do we skip tests, set breakpoints, and professionally mock-out our test components so that we can catch hidden bugs and unexpected scenarios? This page was first published on September 27th 2013 and was last updated on September 28th 2013. So what tool do we use to perform testing? How do I mix all my tests together? Never!

3 ways to get backend data to AngularJS May 27, 2013 – 6:07 pm Tags: AngularJS, JavaScript It seems this is a rather common conundrum with Angular code: You have some backend data, say in PHP or Rails, and you want to output it when rendering the page so that Angular can display it. Since Angular is designed to work more as a single-page app framework, it isn’t immediately obvious how to do this without Ajax, so here I’ll present three ways you can do it. 1. Using ng-init This is probably the single most straightforward way to do it. <div ng-controller="FooCtrl"><span ng-init="hello = true">something</span></div> This example would set $scope.hello to true However, this is not always a suitable option: Since the data goes into an element’s attribute, it can be difficult to do correctly if you need to assign values other than numbers or booleans. So, ng-init is best used with simple values. 2. Simply store your data in a global variable (or a namespaced variable) and access it directly from your controller: 3. For example, In closing

How to make your application assets cachable in Rails Posted over 1 year ago. Visible to the public. Repeats. Every page in your application uses many assets, such as images, javascripts and stylesheets. While ETags will usually prevent the image data from being transmitted multiple times, the browser will open one HTTP request for every asset and block other assets from being loaded until the server responds with 304 not modified. This card describes how you can setup your application in a way that browsers can properly cache your assets. The caching conundrum A caching solution must satisfy two requirements: Allow browsers to cache assets by delivering files with an Expires header in the far future Make sure that when the asset changes, the browser does not see the old, cached version and does make a new request ("stale cache") These requirements are sort of conflicting: If you just told the browser that it may cache images/foo.png for 10 years, and you make a change to foo.png, the browser will only notice the change after 10 years. Rails 3

Related: