簡體   English   中英

灰燼-將數據從服務器檢索到視圖?

[英]Ember -retrieving data from server to a view?

有誰碰巧知道如何顯示來自服務器的數據,現在我已經在這樣的基本把手中顯示了相關模型

{{
           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表單被加倍了JSbin http://jsbin.com/efeReDer/7/edit

今天我想出了...:D Kinda達到目的嗎? 看起來很丑, http://emberjs.jsbin.com/acUCocu/6/edit

基本上,我制作了一個空模型,然后每次循環。 在行動我“ store.create”。空記錄到它。 請給我您的想法:)還有沒有辦法使這些字段獨立? 更改輸入時不會全部更改其內容。 干杯,

克里斯蒂安·

在這里,您可以找到一個我認為您正在問的例子

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

JS

試圖將與應用程序模型相關的實體與實體的顯示方式分開。創建了一個余燼類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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM