保存修改记录的正确方法是什么(EMBER 1.0.0-PRE.4)

时间:2013-01-21 19:24:39

标签: ember-data

我开发了一个小型测试应用程序。加载人员记录 按预期工作。我可以修改firstName和lastName。 在personedit模板中,我有一个触发save方法的动作 App.PersonController。

保存修改记录的最佳方法是什么(保存方法的实现)?

JavaScript的:

window.App = Ember.Application.create();

DS.RESTAdapter.configure("plurals",{"person" : "people"})

App.Store = DS.Store.extend({
revision: 11,
adapter: DS.RESTAdapter.create({namespace: 'restservice'})
});

var attr = DS.attr;

App.Person = DS.Model.extend({
firstName: attr('string'),
lastName: attr('string'),
birthDay: attr('string')
});



 App.Router.map(function(){

    this.resource('people',function(){
        this.resource('person',{path: 'people/:person_id'});
        this.route('edit');
    });

 });


 App.PersonRoute = Ember.Route.extend({

model: function(params){
    console.log('PersonRoute: set Model ==> perform App.Person.find(params.person_id)')
    var p = App.Person.find(params.person_id);
    return p;
},
    renderTemplate: function(){
    this.render('person');
    this.render('personedit',{outlet: 'personedit'});
   }


  });

 App.PersonController = Ember.Controller.extend({
  save: function(){
    console.log('save:  Person:',this.content);

 }
});

App.PeopleEditRoute = Ember.Route.extend({

  model: function(params){
    console.log('EditpersonRoute: set Model ==> perform App.Person.find(params.person_id)')
    var p = App.Person.find(params.person_id);
    return p;
 }

});

模板:

 <script type="text/x-handlebars" data-template-name="application">
    <nav>
        <div class="navbar">
            <div class="navbar-inner">

                <ul class="nav">
                    <li>{{#linkTo "people"}}Personen anzeigen{{/linkTo}}</li>
                    <li>{{#linkTo "page1"}}Page 1 anzeige{{/linkTo}}</li>
                    <li>{{#linkTo "page2"}}Page 2 anzeige{{/linkTo}}</li>
                </ul>
            </div>
        </div>
    </nav>

    <div id="main-content">
        {{outlet}}
    </div>

</script>


<script type="text/x-handlebars" data-template-name="people">
    <div id="people">
        <H3>Personen Liste</H3>
        <ul>
            {{#each  person in controller}}
                <li>
                    {{#linkTo "person" person}} {{person.firstName}} {{person.lastName}}      {{/linkTo}}

                </li>
            {{/each}}

        </ul>

    </div>
    <div class="row-fluid">
        <div class="span6">
            {{outlet}}
        </div>
        <div class="span6">
            {{outlet personedit}}
        </div>
    </div>
</script>


<script type="text/x-handlebars" data-template-name="person">
    <div id="person" class="inline">
        <H3>Person View</H3>

        <dl>
            <dt>Vorname</dt>
            <dd>{{content.firstName}}</dd>
            <dt>Nachname</dt>
            <dd>{{content.lastName}}</dd>
        </dl>
        </div>
    </div>
</script>

<script type="text/x-handlebars" data-template-name="personedit">
    <H3>EDITOR</H3>
    <fieldset>
        <legend>Person</legend>
        <label>Vorname</label>
       {{view Ember.TextField valueBinding="content.firstName"}}
        <span class="help-block">Example block-level help text here.</span>
        <label>Nachname</label>
        {{view Ember.TextField valueBinding="content.lastName"}}
        <button {{action "save" }} class="btn">Save</button>
    </fieldset>

</script>

1 个答案:

答案 0 :(得分:0)

我找到了保存另一个答案的正确方法。我的save方法的实现只需要一行:

App.PersonController = Ember.Controller.extend({
  save: function(){
      console.log('save:  Person:',this.content);
      this.content.get('transaction').commit();
}