angular-server-repeat

Convert server-side rendered content into `ngRepeat` like content
Homepage:
1 person uses it
Author: 0b3a8eb4396d56945e728cd8be7e7817?d=https%3a%2f%2fidenticons.github.com%2f9bda1d56b5e98d9c04f09923d6e37741 restorando
Submitted by: 591992?v=3 gschammah

angular-server-repeat Build Status

Convert server-side repeated content into ngRepeat compatible with some restrictions.

Installation

Include the javascript file and add the ServerRepeat module as a dependency of your app.

Usage

Use the serverRepeat directive in your server-side content using ngRepeat's short syntax.

This will generate a posts array in PostsController scope, and every post will have a child scope with a reference to the current post in the post property, as if rendered client side using ngRepeat.

image

Having a child scope for each member of the collection allows you to "angularize" each item independently. As an example let's hide each post summary and add a link in each post to show it.

Without a child scope, the snippet above would hide all post summaries as expected, but clicking on any link would display all the summaries instead of the selected one.

Data Binding

In the previous example, each child scope has a post empty object. If you need to use a post's data you can use the server-bind directive.

Now we have the html rendered content parsed and populated into the post object in each child scope.

image

Note:

server-bind works as ng-bind, so changing the value of a binded property like the example below will reflect the property update in the DOM.

Adding aditional data that is not rendered in the DOM

The server-bind directive can be used in the same element that uses the server-repeat directive. In this case, it will expect a JSON representation with properties that will be extended to the post object.

Example:

Will produce:

image

Iteration properties

Each child scope have $first, $last, $middle, $even and $odd variables as in ngRepeat.

Motivation

In Restorando we have full client side apps that use AngularJS intensively, and we also have server rendered apps with custom javascript for some pages. Since we had a great experience with AngularJS in the client side apps, we started to slowly remove the legacy javascript files in our "server-side apps" and replace them with reusable angular directives.

We found in the way that we wanted to add functionality to our "repeated" html snippets, but we didn't want to immerse ourselves in a big refactor to render this data client side using ngRepeat. Doing this would also prevent the search engines to index our content.

Searching in the web, we found lots of people trying to accomplish the same thing, like this question, this one and this other one, none of them succesfully.

Using this directive in our own applications made us more agile, and it allowed us to replace our old javascript code into AngularJS faster and easily.

Caveats and restrictions

  • Changes that modify the collection's length (adding or removing items) won't be reflected in the DOM.
  • For the moment you can only bind "flat level" properties using server-bind. Deep level properties will be considered for a future version.

License

Copyright (c) 2015 Restorando

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

comments powered by Disqus
This page was last updated almost 3 years ago.