4

我想在我的 Notes.Application 中构建一个简单的搜索框,用于搜索我的笔记。我正在查看这个网站、Ember 表单和 Google,但没有那么多解决方案。我只找到了两个,它们不适合我的应用程序皱眉。问题是我不知道如何去做。

这是我的应用程序:

<script type="text/x-handlebars" data-template-name="index">  
    <div class="wrap">
      <div class="bar">
      {{input type="text" class="search" placeholder="Where is my bookmark??" value=search action="query"}}

        <div class="bar-buttons">
          <button {{action "addNote"}}> NEW </button>
          <button> HOME </button>
        </div>
      </div>
      <aside>
        <h4 class="all-notes">All Notes {{length}}</h4>
          {{#each item in model}}
            <li>
              {{#link-to 'note' item}} {{item.title}} {{/link-to}}
            </li>
          {{/each}}
      </aside>
      {{outlet}}
    </div> 
</script>

控制器:

Notes.IndexController = Ember.ArrayController.extend ({
search: '',

actions:{
    query: function() {
      // the current value of the text field
      var query = this.get('search');
    },

    addNote: function () {
        this.transitionToRoute('main');
    }
}
});

模型:

    Notes.Note = DS.Model.extend ({
    title: DS.attr('string'),
    body: DS.attr('string'),
    url: DS.attr('string')
    });

    Notes.Note.FIXTURES = [ 
    { 
    id: 1, 
    title:'hello world', 
    body: 'ciao ciao ciao ciao', 
    url: '' 
    }, 
    { 
    id: 2, 
    title: 'javascript frameworks',
    body: 'Backbone.js, Ember.js, Knockout.js', 
    url: '...'
    },
    {
     id: 3,
     title: 'Find a job in Berlin',
     body: 'Monster, beralinstartupjobs.com',
     url: '...'
    }
    ];

但无论如何,这仍然是硬编码数据,稍后将只是用户动态添加的注释。

任何建议都非常感谢。

4

1 回答 1

8

您可以覆盖以下arrangedContent属性IndexController

Notes.IndexController = Ember.ArrayController.extend ({
    search: '',    
    titleFilter: null,
    actions:{
        query: function() {
            // the current value of the text field
            var query = this.get('search');
            this.set('titleFilter', query);
        },
        addNote: function () {
            this.transitionToRoute('main');
        }
    },
    arrangedContent: function() {
        var search = this.get('search');
        if (!search) { return this.get('content') }

        return this.get('content').filter(function(note) {            
            return note.get('title').indexOf(search) != -1;
        })
    }.property('content', 'titleFilter')
});

并在您的模板中使用{{#each item in arrangedContent}}

请参阅此操作http://jsfiddle.net/marciojunior/v966z/

于 2013-11-04T13:11:55.903 回答