我使用Twitter typeahead.js 0.10.5作为建议引擎.它工作得很好,但有一个例外,我不能按照我想要的方式对建议列表进行排序.
举个例子:
var data =[{"id":1,"value":"no need"},
{"id":2,"value":"there is no need"},
{"id":3,"value":"in the need of"},
{"id":4,"value":"all I need"},
{"id":5,"value":"need"},
{"id":6,"value":"needs"},
{"id":7,"value":"all he needs"},
{"id":8,"value":"he needs"},
{"id":9,"value":"they need"},
{"id":10,"value":"you need"}]
var suggestion = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: data,
limit: 20
});
suggestion.initialize();
$('.typeahead').typeahead({
hint: true,
autoselect: true,
highlight: true,
minLength: 1
},
{
name: 'suggestion',
displayKey: 'value',
source: suggestion.ttAdapter(),
templates: {
empty: [
'<div class="empty-message">',
'no suggestion in this map',
'</div>'
].join('\n'),
suggestion: Handlebars.compile('<p><span class="suggestion-text">{{value}}</span></p>')
}
Run Code Online (Sandbox Code Playgroud)
当我输入"need"时,我确实按照数组中的位置获得了建议,但是我想通过输入来命令它,这意味着顺序应该是"需要","需要","我需要的所有"......当输入"他"应该是"他需要","他需要的一切","我需要的一切"等. …
我刚刚开始,Backbone我遇到了一个问题.我有一个很好的安静设置.对于我的大多数GET请求,我收到一个模型集合,但是对于一个我正在加载嵌套关系数据,这会创建嵌套JSON.
在Backbone我有以下Models:
App.Models.Sequence = Backbone.Model.extend({});
App.Models.Layer = Backbone.Model.extend({});
App.Models.Item = Backbone.Model.extend({});
Run Code Online (Sandbox Code Playgroud)
和这些集合
App.Collections.Sequences = Backbone.Collection.extend({
model: App.Models.Sequence,
url: '/api/sequences/'
});
App.Collections.Layers = Backbone.Collection.extend({
model: App.Models.Layer,
url: '/api/layers'
});
App.Collections.Items = Backbone.Collection.extend({
model: App.Models.Item,
url: '/api/items'
});
Run Code Online (Sandbox Code Playgroud)
我将数据加载为JSON:
[
{
"id": "1",
"project_id": "8",
"name": "Seq1",
"layers": [
{
"id": "1",
"name": "Layer11",
"sequence_id": "1",
"items": [
{
"id": "1000000",
"layer_id": "1",
"itemtype_id": "1",
"position": "0"
},
{
"id": …Run Code Online (Sandbox Code Playgroud) 我使用curl将XML文件发布到服务器.Everthing工作完美,但我不知道如何处理响应的最佳方式.我想用simpleXML解析它.有了这个要求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost);
curl_setopt($ch, CURLOPT_POST, 1);
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
print_r($data);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
我得到这个作为响应字符串(浏览器源视图)
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Expires: Sat, 6 May 1995 12:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: JSESSIONID=5FD40C8D8F5EDC85E57AF39E877BE564; Path=/upp/; Secure; HttpOnly
Content-Type: text/xml;charset=UTF-8
Content-Length: 829
Date: Sun, 22 Apr 2012 08:59:10 GMT
<?xml version='1.0' encoding='UTF-8'?>
<authorizationService …Run Code Online (Sandbox Code Playgroud) 我想创建一个包含集合中下拉列表的CompositeView:
我的意见:
var ItemView = Backbone.Marionette.ItemView.extend({
template: '#item-tpl'
});
var CompositeView = Backbone.Marionette.CompositeView.extend({
template: '#comp-tpl',
itemView: ItemView,
itemViewContainer: '#mySelect'
});
Run Code Online (Sandbox Code Playgroud)
我的模板:
<script id = "item-tpl" type="text/template">
<option value="<%= id %>"><%= name %></option>
</script>
<script id = "comp-tpl" type="text/template">
...
<form>
<div class="control-group">
<select id='mySelect'></select>
</div>
</form>
...
</script>
Run Code Online (Sandbox Code Playgroud)
呈现的HTML显示默认的div,它打破了选项列表
<select id="mySelect">
<div>
<option value="5">name 1</option>
</div>
<div>
<option value="6">name 2</option>
</div>
</select>
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点:
<select id="mySelect">
<option value="5">name 1</option>
<option value="6">name 2</option>
</select>
Run Code Online (Sandbox Code Playgroud) backbone.js ×2
bloodhound ×1
curl ×1
json ×1
marionette ×1
nested ×1
php ×1
post ×1
simplexml ×1
sorting ×1
typeahead.js ×1
xml ×1