gpt4 book ai didi

javascript - Ember.js - 未捕获的语法错误 : Unexpected identifier

转载 作者:行者123 更新时间:2023-11-28 04:03:52 24 4
gpt4 key购买 nike

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

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

最佳答案

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

关于javascript - Ember.js - 未捕获的语法错误 : Unexpected identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46867239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com