【问题标题】:Backbone Router not working to display view骨干路由器无法显示视图
【发布时间】:2014-08-05 00:50:23
【问题描述】:

我花了很多时间学习 Backbone 的视图和模型。我现在正在尝试了解路由器以及如何实现它们。我不太清楚为什么我的路由器没有显示PriceView 视图。

我想显示点击链接时在集合模型中找到的价格。我不确定为什么这不起作用。

这是我的代码:http://jsfiddle.net/pHXQg/

这是我的 JavaScript:

// Model
var TheModel = Backbone.Model.extend({
    defaults: {
        name: '',
        shortDescription: '',
        price: ''
    }
});

// Collection
var TheCollection = Backbone.Collection.extend({
    model: TheModel
});

// Instantiate a new collection and populate it with some data.
var aCollection = new TheCollection({
    name: 'Mustang',
    shortDescription: 'Pretty nice even if it is overpriced',
    price: '9999'
});

// View for the title
var TitleView = Backbone.View.extend({
    el: '.js-title',

    initialize: function () {
        this.collection = aCollection;

        return this;
    },

    render: function () {
        var compiledTemplate = _.template( $('#title').html(), { data: this.collection.toJSON() });
        this.$el.html(compiledTemplate);

        return this;
    }
});

// View for the price
var PriceView = Backbone.View.extend({
    el: '.js-price',

    initialize: function () {
        this.collection = aCollection;

        return this;
    },

    render: function () {
        var compiledTemplate = _.template( $('#price').html(), { data: this.collection.toJSON() });
        this.$el.html(compiledTemplate);

        return this;
    }
});

// Instantiate a new TitleView
var titleView = new TitleView();
titleView.render();

// Router
var TheRouter = Backbone.Router.extend({
    routes: {
        'price': 'price' // #price
    },

    price: function () {
        new PriceView();
    }
});

// Instantiate a new Router
var router = new TheRouter();

// Start browser history
Backbone.history.start();

这是我的 HTML 和模板:

<script type="text/template" id="title">
    <h1><%- data[0].name %></h1>
    <a href="#price">Click here to see the price</a>
</script>

<script type="text/template" id="price">
    <p><%- data[0].price %></p>
</script>

<div class="container">

    <div class="row">
        <div class="js-title">

        </div>
    </div>

    <div class="row">
        <div class="js-price">

        </div>
    </div>

</div>

【问题讨论】:

    标签: javascript backbone.js backbone-routing


    【解决方案1】:

    PriceView 的 render() 函数不会被路由器自动调用。

    一种方法是将路由器更改为:

    var TheRouter = Backbone.Router.extend({
        routes: {
            'price': 'price' // #price
        },
    
        price: function () {
            new PriceView().render();
        }
    });
    

    【讨论】:

    • 谢谢!我一直在尝试理解路由。看起来我已经正确实现了吗?
    • 目前,您的路线实现非常基本,但方法是正确的。当路由被命中时,我通常会采取一些小动作链,其中可能包括获取()模型或集合,渲染视图并开始监听来自模型或视图的一些事件。
    • 你在路由器中做那个链吗?例如,如果我要做类似的事情,如果我理解正确的话,我会在 Routher 的 price 方法中进行获取、渲染和侦听。
    • 是的,我会把它们放在那里。
    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多