Web Programming examples

Google Maps,AngularJS

Archives

Ruby on Rails Tutorial microposts feed pagination with AngularJS

1)AngularJS template view

$ vi app/assets/templates/static_pages/home.html.erb

<div ng-controller="HomeCtrl">
  <div ng-show="chkSignin().user.id > 0" class="row">
    <div class="col-sm-4">
.......
    </div>
    <div class="col-sm-8">
      <h3>Micropost Feed</h3>
      <ol class="microposts">
        <li ng-repeat="mp in currentitems">
...........
        </li>
      </ol>
      <pagination total-items="totalitems"
                  ng-model="currentpage"
                  max-size="maxsize"
                  items-per-page="itemsperpage"
                  class="pagination-sm"
                  previous-text="Previous"
                  next-text="Next"
                  first-text="First"
                  last-text="Last"
                  boundary-links="true">
      </pagination>
    </div>
  </div>

2)AngularJS controller

$ vi app/assets/javascripts/mymodule.js.erb

myModule.controller("HomeCtrl", function($scope, flashService, micropostsResource, sessionResource, $filter) {
....
  $scope.submit = function() {
    function success(response) {
      sessionResource.current_user({}, function(response1) {
        var current_user = response1;
        $scope.totalitems = current_user.feed.length;
        feed_items = $filter('orderBy')(current_user.feed,'created_at',true);
        $scope.currentitems = feed_items.slice(start,end);
        flashService.setUser(current_user);
      });
      flashService.push(msg);
      $scope.$emit("$routeChangeSuccess");
    }
    function failure(response) {
........
    }
    micropostsResource.create($scope.micropost, success, failure);
  };
.........
  var feed_items = [];
  $scope.maxsize = 5;
  $scope.itemsperpage = 8;
  $scope.currentpage = 1;
  var start = 0;
  var end = start + $scope.itemsperpage;
  sessionResource.current_user({}, function(response) {
    if (response.feed) {
      var tmp_feed = response.feed;
      $scope.totalitems = tmp_feed.length;
      feed_items = $filter('orderBy')(tmp_feed,'created_at',true);
      $scope.currentitems = feed_items.slice(start,end);
    }
  });
  $scope.$watch('currentpage', function() {
    start = ($scope.currentpage - 1) * $scope.itemsperpage;
    end = start + $scope.itemsperpage;
    $scope.currentitems = feed_items.slice(start,end);
  },true);

});

Ruby on Rails Tutorial microposts feed with AngularJS

1)Rails User model

$ vi app/models/user.rb

  def feed
    Micropost.where("user_id = ?", id)
  end

2)Rails controller

$ vi app/controllers/sessions_controller.rb

  def create
    user = User.find_by(email: session_params[:email].downcase)
    if user && user.authenticate(session_params[:password])
.........
      @user_info = {
        user: user,
        gravatar_url: "https://secure.gravatar.com/avatar/#{gravatar_id}",
        microposts: microposts,
        feed: user.feed
      }
      render json: @user_info, status: :accepted, location: user
    else
......
  def current_user
.......
    @user_info = {
      user: current_user,
      gravatar_url: "https://secure.gravatar.com/avatar/#{gravatar_id}",
      microposts: microposts,
      feed: current_user.feed
    }
    render json: @user_info, status: :accepted

3)AngularJS template view

$ vi app/assets/templates/static_pages/home.html.erb

<div ng-controller="HomeCtrl">
  <div ng-show="chkSignin().user.id > 0" class="row">
    <div class="col-sm-4">
.......
    </div>
    <div class="col-sm-8">
      <h3>Micropost Feed</h3>
      <ol class="microposts">
        <li ng-repeat="mp in chkSignin().feed">
          <a href="/users/{{chkSignin().user.id}}">
            <img alt="{{chkSignin().user.name}}"
                 src="{{chkSignin().gravatar_url}}?s=60"
                 class="gravatar"
            />
          </a>
          <a href="/users/{{chkSignin().user.id}}">{{chkSignin().user.name}}</a>
          <div>{{mp.content}}</div>
          <small class="text-muted">Posted {{mp.created_at | date:'medium'}}</small>
        </li>
      </ol>
    </div>
  </div>