Meteor.render 用法

Meteor.render(function(){
	return Template.template({person:{name:"A",age:1}});
})
渲染的模板,里面的数据源无法进行数据同步。


通过
Meteor.render(function(){
	 _ return Template.template();
})
Template.template.person =function(){
	 _ return Session.get("person")
}


渲染的模板,里面的数据源可以进行数据同步。


验证代码:client/a.html

<head>
  <title>testRenderSync</title>
</head>

<body>
 {{> test}}

 <div id="word"></div>
 <p>===================</p>
</body>

<Template name="test">
	<button>test</button>
</Template>

<template name="hello">
  <h1>Hello World!</h1>
  {{#each people}}
 	<p>{{name}}:{{age}}</p>
  {{/each}}
</template>
<template name="word">
  <h1>===============World!</h1>
  {{#each people}}
 	<p>{{name}}:{{age}}</p>
  {{/each}}
</template>

client/a.js


Template.test.events({
    "click button":function(){
      var people = People.find().fetch();
      $("body").append(Meteor.render(function(){
        return Template.hello({people:people});
      }))
      $("#word").append(Meteor.render(function(){
        return Template.word();
      }));
    }
  });

Template.word.people = function(){
  return People.find().fetch();
}


server/a.js

Meteor.startup(function () {
     var people = [
    {
      name:"A",
      age:1
    },
    {
      name:"B",
      age:2
    }
  ]
    // code to run on server at startup
    if(People.find().fetch().length)
        return;
    for(i in people){
      People.insert(people[i]);
    }

});

modul.js

People = new Meteor.Collection("people");


代码下载: http://download.csdn.net/detail/a6383277/6553697

你可能感兴趣的:(meteor)