【问题标题】:Ember -retrieving data from server to a view?Ember - 从服务器检索数据到视图?
【发布时间】:2014-01-03 09:16:44
【问题描述】:

有人碰巧知道如何显示来自服务器的数据,现在我一直在像这样的基本把手中显示相关模型

{{
           view Ember.Select
           prompt="Organization"
           contentBinding="organizations"
           optionValuePath="content.id"
           optionLabelPath="content.name"
           selectionBinding="selectedOrganization"
}}

但我需要创建一个有多种形式...我使用视图进行复制? 使用视图甚至是正确的路径?!

 {{#each view.anotherField}}
              {{view Ember.TextField value=view.name}}
 {{/each}}

这是我的表单的输出,你可以看到 Organizatons 表单被翻了一番 JSbinhttp://jsbin.com/efeReDer/7/edit

今天我想出了这个... :D 有点达到目的?看起来很难看 http://emberjs.jsbin.com/acUCocu/6/edit

基本上我制作了一个空模型,然后每个循环。 在操作时我“store.create”.empty 记录到它。 把你的想法告诉我:) 还有没有办法让这些领域独立?在更改输入时不会全部更改其内容。 干杯,

克里斯蒂安

【问题讨论】:

    标签: javascript ember.js model controller views


    【解决方案1】:

    在这里你可以找到一个例子,我认为你在问什么

    http://emberjs.jsbin.com/iPeHuNA/1/edit

    js

    试图将与应用模型相关的实体与它们的显示方式分开。创建了一个 ember 类 App.Person,它将保存来自服务器的数据。我没有使用过 ember-data,但如果需要,可以很容易地用 ember-data 表示法替换类,用相应的存储调用等替换虚拟 ajax 调用。

    App = Ember.Application.create();
    
    App.Router.map(function() {
      this.route("persons");
    });
    
    App.IndexRoute = Ember.Route.extend({
      beforeModel: function() {
        this.transitionTo("persons");
      }
    });
    
    App.PersonsRoute = Ember.Route.extend({
      model:function(){
        return $.ajax({url:"/"}).then(function(){/*in url it is required to place the actual server address that will return data e.g. from a rest web service*/
          /*let's imagine that the following data has been returned from the server*/
          /*i.e. two Person entities have already been stored to the server and now are retrieved to display*/
    
          var personsData = [];
          var person1 = App.Person.create({id:1,fname:"Person1",lname:"First",genderId:2});
          var person2 = App.Person.create({id:2,fname:"Person2",lname:"Second",genderId:1});
          personsData.pushObject(person1);
          personsData.pushObject(person2);
    
          return personsData;
        });
      },
      setupController:function(controller,model){
    
    
       /*this could also be retrieved from server*/
        /*let's mimic a call*/
        $.ajax({url:"/",success:function(){
    
          /*This will run async but ember's binding will preper everything.If this is not acceptable, then initialization of lists' values/dictionary values can take place in any earlier phase of the app.  */
    
          var gendersData = [];
          gendersData.pushObject(App.Gender.create({id:1,type:"male"}));
        gendersData.pushObject(App.Gender.create({id:2,type:"female"}));
    
        controller.set("genders",gendersData);
    
        model.forEach(function(person){
    
          person.set("gender",gendersData.findBy("id",person.get("genderId")));
    
    
        });
        }});
    
        controller.set("model",model);
      }
    });
    
    App.PersonsController = Ember.ArrayController.extend({
      genders:[],
      actions:{
        addPerson:function(){
          this.get("model").pushObject(App.Person.create({id:Date.now(),fname:"",lname:""}));
        },
        print:function(){
          console.log(this.get("model"));
        }
      }
    });
    
    App.PersonFormView = Ember.View.extend({
      templateName:"personForm",
      /*layoutName:"simple-row"*/
      layoutName:"collapsible-row"
    });
    
    App.Person = Ember.Object.extend({
      id:null,                                
      fname:"",
      lname:"",
      gender:null
    });
    
    App.Gender = Ember.Object.extend({
      id:null,
      type:null
    });
    

    html/hbs

    创建了一个视图,负责处理每个 App.Person 实例的渲染方式。例如 partiallayouts 已用于适应引导样式,因为我注意到您在示例中使用了一些。

     <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <title>Ember Starter Kit</title>
          <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css">
          <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
        </head>
        <body>
    
          <script type="text/x-handlebars">
            <h2>Welcome to Ember.js</h2>
    
            {{outlet}}
          </script>
    
          <script type="text/x-handlebars" data-template-name="persons">
            {{#each person in this}}
    
            {{view App.PersonFormView}}
    
            {{/each}}
            <br/><br/>
            {{partial "buttons"}}
          </script>
    
          <script type="text/x-handlebars" data-template-name="_buttons">
            <button type="button" class="btn btn-primary" {{action "addPerson"}}>
          add
        </button>
        <button type="button" class="btn btn-primary" {{action "print"}}>
          print results to console
        </button>
    
          </script>
    
          <script type="text/x-handlebars" data-template-name="personForm">
          <div class="row">
          <div class="col-md-6 col-xs-5">
          <div class="form-group">
            <label>First Name</label>
            {{input class="form-control" placeholder="First Name" value=person.fname}}
            </div>
          </div>
    
          <div class="col-md-6 col-xs-5">
            <div class="form-group">
            <label>Last Name</label>
            {{input class="form-control" placeholder="Last Name" value=person.lname}}
            </div>
          </div>
          </div>
         <div class="row">
          <div class="col-md-2 col-xs-4">
          {{
                   view Ember.Select
                   prompt="Gender"
                   content=controller.genders
                   optionValuePath="content.id"
                   optionLabelPath="content.type"
                   selectionBinding=person.gender
                   class="form-control"
                }}
          </div>
          </div>
          <!--</div>-->
    
          </script>
    
          <script type="text/x-handlebars" data-template-name="simple-row">
          <div class="row">
          {{yield}}
          </div>
          <br/><br/>
          </script>
    
          <script type="text/x-handlebars" data-template-name="collapsible-row">
            <div class="panel-group" >
          <div class="panel panel-default">
            <div class="panel-heading">
              <h4 class="panel-title">
                <a data-toggle="collapse" href=#{{unbound person.id}}>
                  person:{{person.fname}}
                </a>
              </h4>
            </div>
            <div id={{unbound person.id}} class="panel-collapse collapse">
              <div class="panel-body">
                {{yield}}
              </div>
            </div>
          </div>
          </div>
          </br>
          </script>
    
          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
          <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
          <script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.1.2.js"></script>
          <script src="http://builds.emberjs.com/tags/v1.2.0/ember.min.js"></script>
    
        </body>
        </html>
    

    【讨论】:

    • 哇,伙计,这看起来太棒了,迫不及待想深入了解这个 =)
    • emberjs.jsbin.com/iPeHuNA/7/edit 为我自己的工作项目编辑了一点。我确实找到了你在那里做的很辛苦的事情:)
    • @kristjan reinhold 太好了,干得好
    • @kristjanreinhold 在我的示例中调用addPerson 时,我将一个空的ember 对象推送到model,它是一个数组(ArrayController)。如果您决定使用此实现,那么在您的 submit/save person 操作中,您将进行 ajax 调用并将数据发送到服务器。如果您想使用 ember-data,那么您将通过 store 管理您的数据,当然您也可以将数据设置为模型到您的控制器,即 emberjs.jsbin.com/iPeHuNA/12 。当您决定save/submit 时,您必须在您的商店中致电save
    • @kristjanreinhold 你从商店得到这个,因为通过调用find 你得到一个承诺。如果你想访问你的数据,你需要在评估承诺时工作,即在调用 print emberjs.jsbin.com/aCeFuve/1/editconsole.log(this.store.findAll('person').then(function(a){console.log(a);})); 时查看控制台。您可能想知道为什么要等待一个承诺来评估我刚刚添加的数据何时已经存在,这就是 ember-data 通过承诺工作的方式。
    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2017-04-18
    相关资源
    最近更新 更多