2

我开发了一个小型测试应用程序。加载人员记录按预期工作。我可以修改名字和姓氏。在 personedit 模板中,我有一个触发 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>
4

1 回答 1

0

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

App.PersonController = Ember.Controller.extend({
  save: function(){
      console.log('save:  Person:',this.content);
      this.content.get('transaction').commit();
}
于 2013-01-22T13:47:13.340 回答