我正在使用 sql 适配器来处理数据库。处理更新的正确方法是什么?
下面的代码在 questionid 为 1624 的条目的数据库中的 sum_points 字段中正确添加了 1 个点,但也出现了错误消息 (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));
}
视图.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;
},
滚动视图中的标签(由数据库填充)以编程方式创建。
解决方案
在 .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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句