ゼロと無限の間に

フリーでオープンソースなJavaScriptとかPHPとか。

ユーザ用ツール

サイト用ツール


javascript:prometheus

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
javascript:prometheus [2012/01/19 22:17] – 作成 dgbadminjavascript:prometheus [2012/04/16 21:45] (現在) – 0.2.0 dgbadmin
行 1: 行 1:
 ====== Titanium Mobileに神の火を! - Prometheus.js ====== ====== Titanium Mobileに神の火を! - Prometheus.js ======
 +
 +コメントと更新履歴は[[http://0-oo.net/log/category/javascript/prometheus/|Prometheus Archive - ゼロと無限の間のログ]]でどうぞ。
  
 {{:javascript:fire_beiz.jp_s05820.jpg?nolink&|}} {{:javascript:fire_beiz.jp_s05820.jpg?nolink&|}}
行 64: 行 66:
  
 === パラメータの共通の初期値を指定できる === === パラメータの共通の初期値を指定できる ===
 +
 +見た目のデザイン等を後から一括して変えたい時に便利
  
 <code javascript> <code javascript>
行 81: 行 85:
 var view = Ti.UI.createView({layout:"horizontal"}); var view = Ti.UI.createView({layout:"horizontal"});
 win.add(view); win.add(view);
-view.add(Ti.UI.createLabel({text:"ラベル1"})); +view.add(Ti.UI.createLabel({text:"ラベル1", fontWeight:"bold"})); 
-view.add(Ti.UI.createLabel({text:"ラベル2"}));+view.add(Ti.UI.createLabel({text:"ラベル2", fontWeight:"bold"}));
 var button = Ti.UI.createButton({title:"ボタン"}); var button = Ti.UI.createButton({title:"ボタン"});
 button.addEventListener("click", function() { button.addEventListener("click", function() {
行 91: 行 95:
 // Prometheus.jsの場合 // Prometheus.jsの場合
 var win = Pr.win(); var win = Pr.win();
 +Pr.params.label.fontWeight = "bold";
 win.add(Pr.view({}, [Pr.label("ラベル1"), Pr.label("ラベル2")]); win.add(Pr.view({}, [Pr.label("ラベル1"), Pr.label("ラベル2")]);
-win.add(Pr.button("ボタン", function() { pr.alertDialog(message:"こんにちはこんにちは!").show(); }));+win.add(Pr.button("ボタン", function() { 
 +    pr.alertDialog(message:"こんにちはこんにちは!").show(); 
 +})); 
 +</code> 
 + 
 + 
 +==== DBにアクセスする ==== 
 + 
 +ローカルのSQLiteに簡単にアクセスできる\\ 
 +DatabaseやResultsetのclose()もしてくれるので楽ちん 
 + 
 +<code javascript> 
 +var db = Pr.db("dbName"); 
 + 
 +// テーブル生成(該当のテーブルが既にある場合は無視される) 
 +db.table("people(id INTEGER, name TEXT, email TEXT)"); 
 + 
 +// SELECT(パラメータは、取得列、テーブル名、取得条件、その他) 
 +var rows = db.select("id, name", "people", { name: "Taro", email: "taro@example.com" }, "ORDER BY id"); 
 +// 戻り値はResultsetオブジェクトではなく配列なので扱いやすい 
 +for (var i = 0; i < rows; i++) { 
 +    var id = rows[i]["id"]; 
 +
 + 
 +// INSERT(パラメータは、テーブル名、登録データ。戻り値は(あれば)連番のキー値) 
 +var id = db.insert("poeple", { id: 1, name: "Taro" }); 
 + 
 +// UPDATE(パラメータは、テーブル名、更新値、更新条件。戻り値は更新レコード数) 
 +var count = db.update("people", { name: "Jiro", email: "jiro@example.com" }, { id: 2, name: "Taro" }); 
 + 
 +// DELETE(パラメータは、テーブル名、削除条件。戻り値は削除レコード数) 
 +var count = db.remove("people", { id: 2, name: "Taro" }); 
 +// メソッド名が"delete"でないので注意! 
 + 
 +// その他のSQLを実行したい場合はTi.Database#execute()と同様に 
 +var results = db.execute(sql, params); 
 +while(rows.isValidRow()){ 
 +    var something = rows.field(0); 
 +    // 略 
 +    rows.next(); 
 +
 +// この場合は勝手にclose()してくれないので注意 
 +results.close();
 </code> </code>
  
行 126: 行 173:
  *  *
  * @see       http://0-oo.net/sbox/javascript/prometheus  * @see       http://0-oo.net/sbox/javascript/prometheus
- * @version   0.1.0+ * @version   0.2.0
  * @copyright 2012 dgbadmin@gmail.com  * @copyright 2012 dgbadmin@gmail.com
  * @license   http://0-oo.net/pryn/MIT_license.txt (The MIT license)  * @license   http://0-oo.net/pryn/MIT_license.txt (The MIT license)
  *  *
- * See also+ * See also the references of Titanium Mobile
  * @see http://tidocs.com/mobile/latest/  * @see http://tidocs.com/mobile/latest/
  * @see http://code.google.com/p/titanium-mobile-doc-ja/wiki/toc  * @see http://code.google.com/p/titanium-mobile-doc-ja/wiki/toc
行 142: 行 189:
  
 /************************************************* /*************************************************
- * The utility functions+ * The wrapper object of Ti.Database
  ************************************************/  ************************************************/
 /** /**
- * Get the opened database+ * Get a wrapper of a database
  * @param String dbName  * @param String dbName
  * @return Object  * @return Object
  */  */
 Pr.db = function(dbName) { Pr.db = function(dbName) {
- return Ti.Database.open(dbName);+ var wrapper = {dbName:dbName, db:null}; 
 +  
 + /** 
 + * Execute SQL (It is the wrapper of Ti.Database.execute()) 
 + * @param String sql 
 + * @param Object (Optional) value(s) of place holder(s) 
 + * @return Object Ti.Database.Resultset or something Ti.Database.execute() returns 
 + */ 
 + wrapper.execute = function() { 
 + if (!this.db) { 
 + this.db = Ti.Database.open(this.dbName)
 + this.db.execute.apply = Function.prototype.apply; 
 +
 +  
 + return this.db.execute.apply(this.db, arguments); 
 + }; 
 + /** 
 + * Close the database 
 + */ 
 + wrapper.close = function() { 
 + this.db.close(); 
 + this.db = null; 
 + }; 
 + /** 
 + * Create a table, if it does not exist 
 + * @param String sql SQL to create a table 
 + */ 
 + wrapper.table = function(sql) { 
 + this.execute('CREATE TABLE IF NOT EXISTS ' + sql); 
 + this.close(); 
 + }; 
 + /** 
 + * Select columns 
 + * @param String columns Columns to select 
 + * @param String from Name of a table 
 + * @param Hash where (Optional) Conditions to select 
 + * @param String ohters (Optional) SQL after WHERE 
 + * @return Array Results 
 + */ 
 + wrapper.select = function(columns, from, where, others) { 
 + var params = this._where(where || {}); 
 + params[0] = "SELECT " + columns + " FROM " + from + params[0] + " " + (others || ""); 
 + var arr = this.rows2arr(this.execute.apply(this, params)); 
 + this.close(); 
 + return arr; 
 + }; 
 + /** 
 + * Insert a row 
 + * @param String table Name of a table 
 + * @param Hash values Values to insert 
 + * @return Number ID of the last inserted row 
 + */ 
 + wrapper.insert = function(table, values) { 
 + var cols = []; 
 + var vals = []; 
 + var params = [""]; 
 +  
 + for (var col in values) { 
 + cols.push(col); 
 + vals.push("?"); 
 + params.push(values[col]); 
 +
 +  
 + params[0] = "INSERT INTO " + table + "(" + cols.join(",") + ") VALUES(" + vals.join(",") + ")"; 
 + this.execute.apply(this, params); 
 + var rowId = this.db.lastInsertRowId; 
 + this.close(); 
 + return rowId; 
 + }; 
 + /** 
 + * Update colulmns 
 + * @param String table Name of a table 
 + * @param Hash set Values to update 
 + * @param Hash where (Optional) Conditions to update 
 + * @return Number the number of the updated rows 
 + */ 
 + wrapper.update = function(table, set, where) { 
 + var sets = []; 
 + var params = [""]; 
 +    
 + for (var col in set) { 
 + sets.push(col + " = ?"); 
 + params.push(set[col]); 
 +
 +  
 + var cond = this._where(where || {}); 
 + params[0] = "UPDATE " + table + " SET " + sets.join(",") + cond.shift(); 
 + this.execute.apply(this, params.concat(cond)); 
 + var affected = this.db.rowsAffected; 
 + this.close(); 
 + return affected; 
 + }; 
 + /** 
 + * Delete rows 
 + * @param String table Name of a table 
 + * @param Hash where (Optional) Conditions to delete 
 + * @return Number The number of the deleted rows 
 + */ 
 + wrapper.remove = function(table, where) { 
 + var params = this._where(where || {}); 
 + params[0] = "DELETE FROM " + table + params[0]; 
 + this.execute.apply(this, params); 
 + var affected = this.db.rowsAffected; 
 + this.close(); 
 + return affected; 
 + }; 
 + /** 
 + * Build a SQL of "WHERE" 
 + * @param Hash hash 
 + * @return Array SQL and values of placeholders  
 + */ 
 + wrapper._where = function(hash) { 
 + var sql = []; 
 + var arr = [""]; 
 +  
 + for (var key in hash) { 
 + sql.push(key + " = ?"); 
 + arr.push(hash[key]); 
 +
 +  
 + if (sql.length) { 
 + arr[0] = " WHERE " + sql.join(" AND "); 
 +
 +  
 + return arr; 
 + }; 
 + /** 
 + * Convert a Ti.Database.Resultset to an array 
 + */ 
 + wrapper.rows2arr = function(rows) { 
 + var arr = []; 
 +  
 + while(rows.isValidRow()){ 
 + var row = {} 
 +  
 + for (var i = 0, cnt = rows.fieldCount; i < cnt; i++) { 
 + row[rows.fieldName(i)] = rows.field(i); 
 +
 +  
 + arr.push(row); 
 +     rows.next(); 
 +
 +  
 + rows.close(); 
 + return arr; 
 + }; 
 +  
 + return wrapper;
 }; };
 +
 +/*************************************************
 + * The utility functions
 + ************************************************/
 /** /**
- * Send the HTTP request and get the response+ * Send HTTP request and get response
  * @param String httpMethod "GET" or "POST"  * @param String httpMethod "GET" or "POST"
  * @param String url  * @param String url
行 183: 行 381:
 }; };
 /** /**
- * Get the width of the display+ * Get width of the display
  * @return Number  * @return Number
  */  */
行 190: 行 388:
 }; };
 /** /**
- * Get the height of the display+ * Get height of the display
  * @return Number  * @return Number
  */  */
行 197: 行 395:
 }; };
 /** /**
- * Merge the hashes+ * Merge hashes
  * @param Hash data1  * @param Hash data1
  * @param Hash data2  * @param Hash data2
行 220: 行 418:
 }; };
 /** /**
- * Add the click listener+ * Add click listener
  * @param Object obj  * @param Object obj
  * @param Function onclick  * @param Function onclick
行 547: 行 745:
  * Create a view  * Create a view
  * @param Hash params  * @param Hash params
- * @param Object or Array children (Optional) The Children of the webView+ * @param Object or Array children (Optional) Children of webView
  * @return Object  * @return Object
  */  */
javascript/prometheus.txt · 最終更新: 2012/04/16 21:45 by dgbadmin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki