1

是否可以在 URL 中使用其他字段?我想使用这个名称,但我首先在特定对象中应用 toLowerCase()。这是我的代码。一切正常,但对象数据没有显示在视图中......有什么想法吗?提前致谢!

<body>

<script type="text/x-handlebars">
    <div class="container">
        <div class="navbar navbar-inverse">
            <a class="navbar-brand" href="#">LayerSpark</a>
            <ul class="nav navbar-nav">
                <li class="active">{{#linkTo 'categories'}}Categories{{/linkTo}}</li>
                <li>{{#linkTo 'help'}}Help{{/linkTo}}</li>
                <li>{{#linkTo 'account'}}Account{{/linkTo}}</li>
            </ul>
        </div>
    </div>
    <div class="container">
                {{outlet}}

    </div>

</script>


<script type="text/x-handlebars" id="categories">

    <div  {{bindAttr class=":row"}}>
        <div {{bindAttr class=":col-lg-4"}}>
            <ul>
                {{#each model}}
                <li>{{#linkTo 'category' slug}}{{name}}{{/linkTo}}</li>
                {{/each}}
            </ul>

        </div>
        <div {{bindAttr class=":col-lg-8"}}>
            {{outlet}}
        </div>
    </div>

</script>

<script type="text/x-handlebars" id="category">
   Post:<br>
    My name is :: {{ categories.name }}<br>
    My id is :: {{id}}<br>
    My slug is :: {{slug}}<br>

</script>

<script type="text/x-handlebars" id="categories/index">
    <p class="text-warning">Please select a Category</p>
</script>

<script type="text/x-handlebars" id="help">
    <p class="text-warning">Help me</p>
</script>

<script type="text/x-handlebars" id="account">
    <p class="text-warning">Your bank account is ...</p>
</script>

应用程序.js

App = Ember.Application.create({
    LOG_TRANSITIONS:true
});
DS.RESTAdapter.configure("plurals", {
    category: "categories"
});
App.Store = DS.Store.extend({
    revision: 12,
    adapter: DS.RESTAdapter.extend({
        url: '/api'
    })
});

App.Router.map(function() {
    this.resource('about');
    this.resource('categories', function() {
        this.resource('category',{path:':category_slug'});
    });
    this.resource('help');
    this.resource('account');
});

App.CategoriesRoute = Ember.Route.extend({
    model: function() {
        return App.Category.find();
    }
});
App.CategoryRoute = Ember.Route.extend({
    setupController: function(controller, category) {
        controller.set('model', category);
    }
});

var attr = DS.attr;

App.Category = DS.Model.extend({
    name: attr('string'),
    slug: function(){
        return this.get('name').toLowerCase();
    }.property('name')
});


Ember.Handlebars.registerBoundHelper('markdown', function(input) {
    return new Handlebars.SafeString(showdown.makeHtml(input));
});

Ember.Handlebars.registerBoundHelper('date', function(date) {
    return moment(date).fromNow();
});
4

1 回答 1

1

你能做的就是钩入serialize你的钩子CategoryRoute并在那里提供蛞蝓。

例子:

App.CategoryRoute = Ember.Route.extend({
  setupController: function(controller, category) {
    controller.set('model', category);
  },
  serialize: function(model) {
    return {
        category_slug: model.get("name").toLowerCase()
    };
  }
});

这样,您还可以删除模型中的 slug 计算属性,然后在您的链接中执行以下操作:

{{#each model}}
  {{#linkTo 'category' this}}{{name}}{{/linkTo}}
{{/each}}

希望能帮助到你。

于 2013-08-02T07:00:01.623 回答