1

我似乎无法更正控制台中的错误。我的代码在文本文件中搜索查询词。如果存在查询词,我希望突出显示文本中的查询词。无法弄清楚这一点。

  var App = Ember.Application.create();   
    App.Router.map(function() {
      // index route is created by default
    });

    App.IndexController = Em.Controller.extend({
        actions: {
            doSearch: function() {
                var self = this;
                self.set('results', null);
                Em.$.get('/patients/search?query='+this.get('searchText')).then(function(data) {
                    for (var i=0;i<data.length;i++) {
                        var docs = data[i].documents;
                        var newDocs = [];
                        for (var j=0;j<docs.length;j++) {
                            parts = docs[j].split(":::");
                            newDocs.push({
                                title: parts[0],
                                date: parts[1],
                                contents: parts[2]
                            });
                        }
                        data[i].documents = newDocs;
                    }
                    self.set('results', data);
                });     
            }
        },

                var queryTerm = this.get('searchText');      //ERROR IN THIS LINE
                Ember.Handlebars.helper('highlightSearchTerm', function (text, queryTerm) {
                    var highlightedText = text.replace(new RegExp(queryTerm, 'gi'), function (str) {
                    return '<mark>' + str + '</mark>';
                });
            return new Ember.Handlebars.SafeString(highlightedText);
            });  
    });
4

1 回答 1

0

helper应该是它自己的一段代码。

var App = Ember.Application.create()

App.Router.map(function() {
  // index route is created by default
})

App.IndexController = Em.Controller.extend({
  actions: {
    doSearch: function() {
      var self = this

      self.set('results', null)

      Em.$.get('/patients/search?query=' + this.get('searchText')).then(function(data) {
        for (var i = 0; i < data.length; i++) {
          var docs = data[i].documents

          var newDocs = []

          for (var j = 0; j < docs.length; j++) {
            parts = docs[j].split(':::')

            newDocs.push({title: parts[0], date: parts[1], contents: parts[2]})
          }

          data[i].documents = newDocs
        }

        self.set('results', data)
      })
    }
  }
})

Ember.Handlebars.helper('highlightSearchTerm', function(text, queryTerm) {
  var highlightedText = text.replace(new RegExp(queryTerm, 'gi'), function(str) {
    return '<mark>' + str + '</mark>'
  })

  return new Ember.Handlebars.SafeString(highlightedText)
})

然后你在你的模板中使用帮助器,比如

{{highlightSearchTerm searchText}}
于 2017-10-23T08:52:15.503 回答