小编aru*_*ons的帖子

如何观察向数组添加元素

我想观察添加元素到数组.以下是测试程序.

<!-- library load -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.6.1.min.js"%3E%3C/script%3E'))</script>
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.5.min.js"></script>

<script type="text/x-handlebars">
  {{#each App.ArrayController.array}}
    {{foo}}
  {{/each}}
  <button onclick="App.ArrayController.addElement();">add</button>
</script>
<script type="text/javascript">
  var App = Em.Application.create();
  App.ArrayController = Em.Object.create({
    array: [{foo:1}, {foo:2}, {foo:3}],
    addElement: function() {
      this.array.pushObject({foo:4});
    },
    elementAdded: function() {
      alert('ok'); // not invoked...
    }.observes('array')
  })
</script>  
Run Code Online (Sandbox Code Playgroud)

但是当调用addElement时,不会调用elementAdded ...如何观察添加元素?

ember.js

14
推荐指数
2
解决办法
2万
查看次数

使用emberjs初始化后如何聚焦?

我是Ember.js的新手.我想在初始化后专注于TextField(在示例中,id ="text"),但是在ready函数中,不能使用focus方法...

<body>
  <!-- library load -->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.6.1.min.js"%3E%3C/script%3E'))</script>
  <script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.5.min.js"></script>

  <script type="text/x-handlebars">
        {{view Em.TextField id="text"}} // want to focus it.
  </script>
  <script type="text/javascript">
    var App = Em.Application.create();
        App.ready = function() {
            $('#text').focus(); // does'nt work.
        }
  </script>
</body>
Run Code Online (Sandbox Code Playgroud)

ember.js

10
推荐指数
1
解决办法
3321
查看次数

我可以访问{{#with}}范围内的视图属性吗?

我希望在{{#with}}范围内切换元素的可见性,具体取决于视图属性的值.我可以访问{{#with}}中的{{action}}中的查看属性,但我无法访问{{#with}中的查看属性.

以下是示例程序.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.6.1.min.js"%3E%3C/script%3E'))</script>
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.5.js"></script>

<script type="text/javascript">
  var App = Em.Application.create();
  App.Test = Em.View.extend({
    content: {
      description: 'names',
      names: [{name: 'bob'}, {name: 'john'}]
    },
    viewDetail: false,
    toggleDetail: function() {
      this.set('viewDetail', true);
    }
  })        
</script>

 <script type="text/x-handlebars">
  {{#view App.Test}}
    {{#with content}}
      {{description}}<br/>
      {{#each names}}
        {{name}}
      {{/each}}
      <br/>
      <button {{action toggleDetail}}>toggle Detail</button> <!-- can access toggleDetail -->
      {{#if viewDetail}} <!-- but can not access viewDetail... -->
        …detail Description...
      {{/if}}
    {{/with}}
  {{/view}}
</script>
Run Code Online (Sandbox Code Playgroud)

我可以访问{{#with}}范围内的查看属性吗?

ember.js

3
推荐指数
1
解决办法
821
查看次数

标签 统计

ember.js ×3