无法在Handlebar中获取json对象数组

时间:2015-01-24 19:52:00

标签: ruby-on-rails json handlebars.js

您好我是车把和导轨的新手,我正在尝试渲染车把模板,该模板消耗了一个具有2个值的json对象

json对象将列表数组对象作为字符串,这就是为什么我无法在handlebar模板中获取其值,如下面的代码所示

Object {type: "followers", list: "[{"user_id":33,"user_name":"rajesh","first_name":"avatars/000/000/033/dummyavatar.png?1421501369"}]"}

车把模板

{{#compare this.type '==' 'followers'}}
{{#each this.list}}
  <div class="col-md-8 col-md-offset-2 project-box">
    <div class="col-md-8 align-center">
      <a href="/project/{{profile_url}}" class="avatar-md">
        <img src="{{image_url}}" class="avatar-md">
      </a>
      <span>
      {{user_name}}
      </span>
    </div>
    <div class="col-md-4 clearfix action">
      <a href="/project/{{project_url}}" class="btn btn-primary pull-right">
        follow
      </a>
    </div>
  </div>
{{/each}}

{{/比较}}

控制器

def getFollowersList
    objFollowNetwork = FollowNetwork.new(session[:user_id])
    @listOfFollowers = objFollowNetwork.getListOfFollowers
    #@listOfFollowers[0].image_urlA
    objHash = Hash.new
    objHash['type'] = 'followers'
    objHash['list'] = @listOfFollowers.to_json(:methods => :image_url) 
    render :json => objHash
    # render :json => @model.to_json(:only => [:id,:name,:homephone,:cellphone], :methods => [:avatar_url])
  end

Ajax调用函数

    function getFollowerList() {
  $.ajax({
    url: "/getFollowers",
    method: 'get',
    dataType: 'json',
    success :function(result){
      debugger;
      context = JSON.parse(result);
      followerList = Handlebars.templates['followersCommonTemplate'](result);
      $('#followers-sections').html(followerList);
    }
  });
}

有没有办法在Handlebar中获取列表对象数组?

1 个答案:

答案 0 :(得分:0)

为了实现这一点,我尝试将列表数组制作成json对象并将其分配给result.list。

result.list = JSON.parse(result.list)
followerList = Handlebars.templates['followersCommonTemplate'](result);

这将创建一个具有列表作为数组对象的json对象!