在Backbone Collection中覆盖解析,无法呈现结果

时间:2017-05-22 12:47:59

标签: javascript backbone.js marionette

在查看前面的答案ref:a previous answer之后,我正在寻找覆盖解析,以便我可以提取一个嵌套的项集合,并使用我的json响应中的一些其他属性。

如果我只有我的json中的项目它可以在不重写解析的情况下工作。如果我试图覆盖解析,那么我看不到我的项目渲染,但它们在数据中存在。

这是一个古老的项目,但我真的需要帮助解决这个问题......

var ContentListingBlock = ViewController.extend({
        name: 'module/content-listing-block/view/main-view-controller',
        template: templates['module/content-listing-block/main-view-controller'],
        expectedRegions: ['items-region'],

        contents: '',
        json: '',

        onStart: function() {
            console.log('ContentListingBlock')
            var $this = this;

            var Item = Backbone.Model.extend()
            $this.ItemCollection = Backbone.Collection.extend({
                model: Item
            })

            var ContentListing = Backbone.Collection.extend({
                url: 'http://localhost:3000/data',
                parse: function(response) {
                    response.items = new $this.ItemCollection(response.items)
                    return response
                }
            });

            $this.contents = new ContentListing()

            $this.showResults()

            $('.button-fetch').on('click', function(){
                $this.fetchItems()
            })

        },

        fetchItems: function() {
            var $this = this

            this.contents.fetch({
                data: { page: 1, size: 6 },
                processData: true
            }).then(function(){
               console.log('data:', $this.contents)
            })
        },

        showResults: function() {
            var $this = this

            var ItemCollectionView = Marionette.CollectionView.extend({
                tagName: 'section',
                childView: ItemView
            })

            this['items-region'].show(new ItemCollectionView({ collection: $this.contents.items }))
        }
    })

    return ContentListingBlock

这是我的模特

{
    "data": {
        "pageSize": 6,
        "pagesNum": 3,
        "items": [
            {
                "id": 1,
                "imagePath": "image.jpg",
                "categoryType": "A",
                "storyTitle": "Story Title 2",
                "content": "Story content...",
                "quoteFlag": "false",
                "author": "Paul Dirac"
            },
            {
                "id": 2,
                "imagePath": "image.jpg",
                "categoryType": "B",
                "storyTitle": "Story Title 3",
                "content": "Story content...",
                "quoteFlag": "false",
                "author": "Nikola Tesla"
            },
            {
                "id": 3,
                "imagePath": "image.jpg",
                "categoryType": "B",
                "storyTitle": "Story Title 4",
                "content": "Story content...",
                "quoteFlag": "false",
                "author": "Max Planck"
            }
        ]
    }
}

0 个答案:

没有答案