首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拉出关系数据的JSON子数组并放入主干模板

拉出关系数据的JSON子数组并放入主干模板
EN

Stack Overflow用户
提问于 2013-08-16 15:30:45
回答 1查看 241关注 0票数 1

我从服务器检索以下Quotation结果的JSON数组。

代码语言:javascript
复制
  [{
     "id":"1",
     "name":"A",
     "year":"FY2013-2014",
     "Project":{
         "id" : "17",
         "name" : "ABC"
      },
     "Site":[
        {
         "id" : "12",
         "name" : "Victoria"
        },
        {
         "id" : "13",
         "name" : "Zulu"
        },
     ],
  },
  {
     "id":"2",
     "name":"B",
     "year":"FY2013-2014",
     "Project":{
         "id" : "19",
         "name" : "DEF"
      },
     "Site":[
        {
         "id" : "12",
         "name" : "Victoria"
        },
        {
         "id" : "14",
         "name" : "Mambo"
        },
     ],
  }
  ]

上面是一个由两个字典组成的数组。

每个字典表示一个Quotation

我知道如何使用Quotation id和Quotation名称来显示ItemTemplate

代码语言:javascript
复制
 <script type="text/html" id="resultItemTemplate">
     <td><%= id %></td><!-- this is the Quotation id -->
     <td><%= name %></td><!-- this is the Quotation name -->
  </script>

我想知道如何显示Project的子字典结果,并在同一个resultItemTemplate中迭代Site字典的子数组。

我看到了这个link,但它不适合字典的子数组。

我正在使用以下库:

代码语言:javascript
复制
files[] = libs/jquery/1.10.2/jquery.js
files[] = libs/underscore/1.4.4/underscore.min.js
files[] = libs/backbone/0.9.10/backbone.min.js
files[] = libs/json/json2.js
files[] = libs/backbone/backbone.paginator.min.js

这是解析JSON数据的PaginatedCollection代码。

代码语言:javascript
复制
(function (collections, model, paginator) {

    // Create a new collection using one of Backbone.Paginator's
    // pagers. We're going to begin using the requestPager first.

    collections.QuotationPaginatedCollection = paginator.requestPager.extend({
        // As usual, let's specify the model to be used
        // with this collection
        model: model,

        // We're going to map the parameters supported by
        // your API or backend data service back to attributes
        // that are internally used by Backbone.Paginator. 

        // e.g the NetFlix API refers to it's parameter for 
        // stating how many results to skip ahead by as $skip
        // and it's number of items to return per page as $top

        // We simply map these to the relevant Paginator equivalents

        // Note that you can define support for new custom attributes
        // adding them with any name you want

        paginator_core: {
            // the type of the request (GET by default)
            type: 'GET',

            // the type of reply (jsonp by default)
            dataType: 'jsonp',

            // the URL (or base URL) for the service
            url: '/quotations?'
        },

        paginator_ui: {
            // the lowest page index your API allows to be accessed
            firstPage: 1,

            // which page should the paginator start from 
            // (also, the actual page the paginator is on)
            currentPage: 1,

            // how many items per page should be shown
            perPage: 10,

            // a default number of total pages to query in case the API or 
            // service you are using does not support providing the total 
            // number of pages for us.
            // 10 as a default in case your service doesn't return the total
            totalPages: 10
        },

        server_api: {
            // the query field in the request
            'q': '',

            // we need to indicate whether to display or not
            'show' : 'no',

            // number of items to return per request/page
            'top': function() { return this.perPage },

            // how many results the request should skip ahead to
            // customize as needed. For the Netflix API, skipping ahead based on
            // page * number of results per page was necessary.
            'page': function() { return (this.currentPage) },

            // field to sort by
            'sort': function() {
                if(this.sortField === undefined)
                    return 'id';
                return this.sortField;
            },

            // sort field by direction
            'direction': function() {
                if(this.dirField === undefined)
                    return 'asc';
                return this.dirField;
            },

            // what format would you like to request results in?
            //'$format': 'json',

            // custom parameters
            '$inlinecount': 'allpages',
            '$callback': '?'
        },

        parse: function (response) {
            // Be sure to change this based on how your results
            // are structured (e.g response.products is CakePHP-Backbone specific)
            var products = response.data;
            var paging = response.paging;

            this.totalPages = paging.pageCount;
            this.totalRecords = paging.count;

            return products;
        },

        initialize: function(options) {
            if (options != undefined) {
                this.options = options;
                if ('proj' in this.options) {
                    this.server_api.proj = this.options.proj;
                }
            }
        },

    });

})( app.collections, app.models.Quotation, Backbone.Paginator);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-16 16:29:09

要显示我使用的字典数组:

代码语言:javascript
复制
<% _.each(Site, function (site) { %>
    <a href="#"><%= site.name %></a>
<% }); %>

为了显示我使用的子字典,我使用:

代码语言:javascript
复制
<td><%= Project.name %></td>

所以它的工作方式是这样的:

代码语言:javascript
复制
<script type="text/html" id="resultItemTemplate">
    <td><%= id %></td><!-- this is the Quotation id -->
    <td><%= name %></td><!-- this is the Quotation name -->
    <td><%= Project.name %></td>
    <td>
    <% _.each(Site, function (site) { %>
        <a href="#"><%= site.name %></a>
    <% }); %>
    </td>
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18277006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档