1

我有一个简单的模型数组,它们显示在列表中(路径:/things)。模型从 REST-API 加载。

在嵌套路线中,我具有添加新模型的功能。(路径:/things/add)。新模型通过 REST-API 持久化。

添加新模型后,我transitionTo('things')会返回列表。

按照 ember 文档,通过使用“transitionTo” ,非动态路由不会调用model钩子和setupController-Hook。

使用时在非动态路径上刷新模型的正确方法是什么transitionTo?或者:在非动态路径上重新加载模型而不使用的最佳方法是什么transitionTo

应用程序.js

// App Init
App = Ember.Application.create();

// Routes
App.Router.map(function() {
    this.resource('things', function() {
        this.route('add');
    })
});

App.IndexRoute = Ember.Route.extend({
    redirect : function() {
        this.transitionTo('things');
    }
});

App.ThingsRoute = Ember.Route.extend({
    model : function(param) {
        return App.Thing.findAll();
    },
});

App.ThingsAddRoute = Em.Route.extend({
    setupController : function(controller) {
        controller.set('content', App.Thing.create());
    }
});

// Models
App.Thing = Ember.Object.extend({
    name : null,
    description : null
});

App.Thing.reopenClass({
    findAll : function() {
        var result;
        $.ajax({
            url : 'http://path/app_dev.php/api/things',
            dataType : 'json',
            async : false,
            success : function(data) {
                result = data.things;
            }
        });
        return result;
    },
    save : function(content) {
        console.log(content);
        $.ajax({
            type : 'post',
            url : 'http://path/app_dev.php/api/things',
            data : {
                name : content.name,
                description : content.description
            },
            async : false
        });
    }
});

// Controller
App.ThingsAddController = Em.ObjectController.extend({
    add : function() {
        App.Thing.save(this.content);
        this.transitionToRoute('things');
    },
    cancelAdding : function() {
        this.transitionToRoute('things');
    }
});

索引.html

<script type="text/x-handlebars">
<div class="container">
    <div class="row">
        <div class="span12">
            <h1>List of things</h1>
        </div>
        {{outlet}}
    </div>
</div>
</script>

<script type="text/x-handlebars" data-template-name="things/add">
<div class="span12">
        <form class="form-horizontal">
        <fieldset>
            <div id="legend">
                <legend class="">Add new thing</legend>
            </div>

            <!-- Name -->
            <div class="control-group">
                <label class="control-label" for="name">Name</label>
                <div class="controls">
                    {{view Ember.TextField id="name" placeholder="Enter Name" valueBinding="name"}}
                </div>
            </div>

            <!-- Description -->
            <div class="control-group">
                <label class="control-label" for="description">Description</label>
                <div class="controls">
                    {{view Ember.TextArea id="description" placeholder="Enter description" valueBinding="description"}}
                </div>
            </div>

            <!-- Submit -->
            <div class="control-group">
                <div class="controls">
                    <button class="btn btn-success" {{action add}}>Save</button>
                    <button class="btn" {{action cancelAdding}}>Cancel</button>
                </div>
            </div>

        </fieldset>
        </form>
</div>
</script>    

<script type="text/x-handlebars" data-template-name="things">
<div class="span12">
    <div class="btn-toolbar">
        <div class="btn-group">
            {{#linkTo things.add}}<i class="icon-plus"></i> add new thing{{/linkTo}}
        </div>
    </div>
</div>
{{outlet}}  
<div class="span12">
    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped ">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Description</th>
            </tr>
        </thead>
        <tbody>
        {{#each item in model}}
            <tr>
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.description}}</td>
            </tr>
        {{/each}}
        </tbody>
    </table>
</div>
</script>
4

1 回答 1

2

So if you were using ember-data, a side effect of saving the record would be that the results of findAll() get updated. You can accomplish the same by either manually updating the array or triggering a refresh when a new record is added. In either case, suggest doing that from ThingsAddController's add fx. For example:

App.ThingsAddController = Em.ObjectController.extend({
  needs: [things],
  add : function() {
    newThing = App.Thing.save(this.content);
    this.get('controllers.things').pushObject(newThing);
    this.transitionToRoute('things');
  },
});
于 2013-05-29T17:47:55.280 回答