2012-01-02
■[JavaScript]firebugの計測関数(console.time/console.timeEnd)を自動挿入
firebugの計測関数(console.time/console.timEnd)をつかってメソッドの実行時間の計測するときなどに、計測関数を挿入忘れと削除忘れしないために自動的に挿入するためのコードです
開発のときには、ブックマークレットで呼び出しておいて使うと便利かも(しません)。
フレームワーク部分(interceptor.js)
aop = {}; aop.interceptors = {}; aop.interceptors.TimeInterceptor = function(){}; aop.interceptors.TimeInterceptor.prototype.create = function(lambda) { var module = this.module; return function() { console.time(module); lambda.apply(this, arguments); console.timeEnd(module); }; }; aop.aspect = function(namespace, interceptor) { namespace = namespace.split('.'); // 1. foo.barのオブジェクトを取得 var obj = window; for (var i = 0, f = namespace.length; i < f;i++) { obj = obj[namespace[i]]; }; // 2. foo.barのオブジェクトを全捜査してフィールドがfunctionのときに、 // 既存のfunctionをInterceptorでラップしている。 namespace = namespace.join('.'); for (i in obj) { var field = obj[i]; interceptor.module = namespace + '.' + i; if (typeof(field) !== 'function') { continue; }; // 既存のfunctionのプロパティなどをInterceptorでラップしたfunctionにコピーしている。 var newField = obj[i] = interceptor.create(field); for (var j in field) { newField[j] = field[j]; }; }; };
動かすためのサンプル
foo = {}; foo.bar = {}; foo.bar.hoge = function() { alert('hoge'); }; // foo.barにTimeInterceptorを適用 aop.aspect('foo.bar', new aop.interceptors.TimeInterceptor()); foo.bar.hoge();
課題とか
- prototypeに対応は未考慮
2011-10-05
■[JavaScript]fx7でresizeTo/resizeBy/moveTo/moveByがno longer apply to main windowな件
タイトル通りです。fx7からresizeTo/resizeBy/moveTo/moveByがメソッドとしては定義されていますが動作しなくなりました…。じゃなくて、no longer apply to main windowになりました。
これ系...
window.resizeTo(width, height); window.resizeBy(width, height); window.moveTo(x, y); window.moveBy(x, y);
Firefox 7 for developersによれば
window.resizeTo, window.resizeBy, window.moveTo , and window.moveBy no longer apply to the main window.
https://developer.mozilla.org/en/Firefox_7_for_developers#DOM
bugzillaでの議論
Bug 565541 – Web sites shouldn’t be allowed to resize main window
ここで議論されています。「websiteには、main windowのリサイズを許可すべきではない」
Comment53
For the most part, this only breaks web pages that are using this in ways that already break things. We are still allowing popup windows to resize themselves. We simply aren't allowing tabs in full-UI main browser windows to resize themselves, because they affect other tabs. In other words, we're actually fixing a lot more breakage than we're causing.
https://bugzilla.mozilla.org/show_bug.cgi?id=565541#c53
んー。読み間違いかなんなのか。スレッドを追ってもなんか話がつながっていかないんだよな…。
追記
- 後、最初のエントリでーfirefox7でwindow.resizeTo/window.resizeByが動かなかったと記載してありましたが、「fx7+firebug」の環境ではでした。
2011-08-03
■[PHP]Smartyのblockプラグインで変数を使う方法
サンプルソース
実行用のPHP
<?php require_once 'Smarty.class.php'; $smarty = new Smarty(); $smarty->assign('array', array( array('foo'=>'foo1', 'bar'=>'bar1'), array('foo'=>'foo2', 'bar'=>'bar2') )); $smarty->display('test.html');
テンプレート(test.html)
<{sample item=$array}> <{$item.foo}> -> <{$item.bar}> <{/sample}>
block.sample.php
<?php function smarty_block_sample($params, $content, &$smarty, &$repeat) { if(is_null($content)) { $smarty->__temp__ = $params['item']; $item = array_shift($smarty->__temp__); $smarty->assign('item', $item); return; } $item = array_shift($smarty->__temp__); if(is_array($item)) { $smarty->assign('item', $item); $repeat = true; } else { $repeat = false; } return $content; }
$repeatが参照渡しになっているのがポイントですね。
ToDo
雑感
2011-05-30
■[Flex]FlexでのMVC設計による開発パターン - その1 -
d:id:shogo4405:20110521:1305947042をMVC的な考え方を踏襲した設計パターンです。ショッピングカート系のアプリケーションを開発するという前提で、僕ならこう設計していくよー。という話。
Modelの設計
WebServiceやHTTPServiceと連携すること考慮しておきます。ただし今回は簡単のためショッピングカートクラスの中身は次のようにシンプルにWebServiceの連携を考えない。
加えてのポイントは[Bindable]を忘れないことと、Singletoneパターンを活用すること。
myapp.controls.ShoppingCart
数量しか持っていないショッピングカート(何を買うんだろう…)で。addメソッドで数量を追加するというイメージ。
{
[Bindable]
public class ShoppingCart
{
private static var instance:ShoppingCart = new ShoppingCart();
// 買い物相手も数量
public var quantity:int;
// コンストラクタ
public function ShoppingCart() {
quantity = 0;
};
// 買い物かごのアイテムを追加
public function add():void {
quantity++;
};
public static function getInstance():ShoppingCart {
return instance;
};
};
};
Controllerの設計
mx.controller.*を原則拡張するように設計します。IMXMLObjectを拡張して専用のControllerクラスをつくる…みたいなことはあえてしません。Webだったら/foo/aMethod or /foo/bMethodみたいに、メソッド数が少なくて済むけど。
GUI系だったらContollerの数が例えばボタンのイベント数。インプットフォームのイベント数と膨大になりがちになるのでこれも分割。mx.controller.*を拡張することで適当な大きさのControllerに分割します。<mx:Buttun click="controller.buttun1Click(e);" />みたいなこと必要?
package myapp.controls
{
import flash.events.MouseEvent;
import mx.controls.*;
import myapp.models.ShoppingCart;
public class AddButtun extends Button
{
private var model:ShoppingCart;
public function AddButtun()
{
this.model = ShoppingCart.getInstance();
this.addEventListener(MouseEvent.CLICK, click);
};
private function click(e:*):void{
model.add();
};
};
};
Viewの設計
もちろん、ここはmxmlを前提にして設計。mx.containers.*系はクラスを拡張して利用してあげること。appendChildとかメンドクサイのでしません。
Main.mxml
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="myapp.controls.*" > <mx:Script> <![CDATA[ import myapp.models.*; [Bindable] public var model:ShoppingCart = ShoppingCart.getInstance(); ]]> </mx:Script> <mx:TextInput text="{model.quantity}" /> <local:AddButtun label="addCart" /> </mx:Application>
というわけで
上記のようにコーディングしていくと、View → Model ← Controllerで、Modelを媒介としてControllerでModelを更新してあげれば、Viewが勝手に更新されていく。実質、ControllerはViewのことを知らなくてもいい。Viewは、Controllerを知らなくていいみたいなことことができる。
これを実現している、Flexの[BIndable]で、[Bindable]を利用すると結合ぐあいがゆるく分離できるので良いです。
2011-05-29
■[JavaScript]Greasemonkeyでjquery-uiを使うときのスニペット
jquery-uiを@requireしてもエラーで怒られたりするのでよく調べたらこんなんでいけるらしい。
// ==UserScript== // @resource jquery http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js // @resource jqueryui http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js // ==/UserScript== (function() { var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.type = 'text/javascript'; var jQuery = GM_getResourceText('jquery'); var jQueryUI = GM_getResourceText('jqueryui'); script.innerHTML = jQuery + jQueryUI; head.appendChild(script); $ = unsafeWindow.$; })(); $(document).ready(function(){ // -- });
