私はデータベースを操作するためにSQLアダプターを使用しています。更新を処理する適切な方法は何ですか?
以下のコードは、質問ID 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));
}
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();
データベースだけでなく、コレクションとモデルを使用しているようです。最も重要なことについては、クエリをまったく使用する必要はありません。モデルに関する関連ドキュメントをご覧ください。
あなたの場合は、適切なID(またはそれらを取得するためのバックボーンメソッド)を使用してモデルを取得するだけです。
var model = $.fetchedTranslations.get('123');
model.set({sum_points: model.get('sum_points') + 1});
model.save();
コレクションはバックボーンベースであるため、バックボーンドキュメントも確認することをお勧めします。これにより、ID、フィルターなど以外のプロパティを取得する方法を確認できます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加