我正在使用sql适配器来处理数据库。处理更新的正确方法是什么?
下面的代码正确地将1个点添加到带有questionid 1624的条目的数据库中的sum_points字段,但是还有一个错误消息(rs.fieldCount)即将出现并且最后的console.log未被执行
var fetchedTranslations = $.translationData;
fetchedTranslations.fetch({
query: { statement: 'UPDATE "translationsCollection" SET "sum_points" = "sum_points" + ? WHERE "questionid" = ?', params: [1,1624] } //correctly changes the value but results in error message (rs.fieldCount)
});
console.log("fetchedTranslations: " + JSON.stringify(fetchedTranslations));
}
views.xml:
<Alloy>
<!--<Model src="translationsCollection"/>-->
<Collection src="translationsCollection" instance="true" id="translationData"/>
<Window id="learnQuestions" title="Learn">
<View id="learnQuestionContainer" layout="vertical" width="98%" borderWidth="2" borderRadius="5" backgroundColor="Alloy.CFG.design.backgroundColor" borderColor="#E6000000" height="98%">
<View id="ratingBar" height="Ti.UI.SIZE" backgroundColor="orange" left="1%" width="98%">
<Label id="pointsCounter" width="30%" left="30%" height="Ti.UI.SIZE" top="0" text="pointsCounter" onClick="calculatePoints" top="0"/>
<!-- calculatePoints triggers the points update function-->
</View>
<ScrollView id="learnQuestionsContainer" layout="vertical" height="Ti.UI.SIZE" dataCollection="$.translationData" dataTransform="transformFunction">
</ScrollView>
</View>
</Window>
trasnlationCollection.js(在模型文件夹中)
exports.definition = {
config: {
columns: {
"questionid": "real",
"question": "text",
"answer": "text",
"difficulty": "real",
"language": "text",
"sum_points": "real",
"sum_words": "real"
},
adapter: {
type: "sql",
collection_name: "translationsCollection",
idAttribute : "questionid"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
idAttribute : "questionid",
// extended functions and properties go here
});
return Model;
},
Scrollview中的标签(由数据库填充)是以编程方式创建的。
解
在.xml文件中添加了模型src,并按照Rene的建议获取并设置值:
<Model src="translationsCollection"/>
var fetchedTranslations = $.translationData;
var model = fetchedTranslations.get('1624');
console.log("model fetched: " + JSON.stringify(model));
model.set({sum_points: model.get('sum_points') + 1});
model.save();
答案 0 :(得分:2)
您似乎正在使用集合和模型,而不仅仅是数据库。对于大多数重要的事情,您根本不需要使用查询。只需查看有关模型的相关docs。
在您的情况下,只需使用正确的ID(或任何骨干方法获取它们)获取模型
var model = $.fetchedTranslations.get('123');
model.set({sum_points: model.get('sum_points') + 1});
model.save();
由于集合是基于主干的,我建议也检查backbone docs,这样你就可以看到除ID,过滤器等之外的任何其他属性如何获取。