Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build search box searching with Ember.js

I would like to build an easy search box in my Notes.Application for searching through my notes. I am looking in this website, Ember form and Google and there are not that many solutions. I found just two and they don't fit in my App frowning. The problem is that I don't have a general idea how to do it.

Here is my app:

<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>

Controller:

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');
    }
}
});

Model:

    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: '...'
    }
    ];

But anyway this is still hard-coded data, later will be just notes added dynamically from the user.

Any suggestion is really appreciated.

like image 305
Giorgia Sambrotta Avatar asked Feb 15 '23 19:02

Giorgia Sambrotta


1 Answers

You can do this overriding the arrangedContent property of 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')
});

And in your template use {{#each item in arrangedContent}}

See this in action http://jsfiddle.net/marciojunior/v966z/

like image 156
Marcio Junior Avatar answered Feb 17 '23 08:02

Marcio Junior