git reset --hardで新規ファイルの扱い

$ git --version
git version 1.8.3.2

$ touch newfile
$ git status -s 
?? newfile

$ git reset --hard
$ git status -s
?? newfile

このようにまだgitに管理されてないファイルはgit reset --hardしても削除されない。これはよい。

ただ、次のように、インデックスに追加した後にgit reset --hardするとファイルが消える。

$ git add newfile
$ git status -s
A  newfile

$ git reset --hard
$ git statsu -s # no output
$ ls newfile
ls: newfile: No such file or directory

なんでこうなるのかイマイチよくわからない。

ちなみにcheckoutだとインデックスに追加してようがしていまいが何も影響しない。

$ git status -s 
?? newfile

$ git checkout HEAD .
$ git status -s 
?? newfile

$ git add newfile
$ git status -s
A  newfile

$ git checkout HEAD .
$ git status -s
A  newfile

新規ファイルの扱い以外は`reset --hard`と`checkout HEAD .`は同じ挙動だと思う。たぶん。

AngularJSの$httpにX-Requested-Withをつける

AngularJSでHTTP Requestする際に、1.1.1より前まではX-Requested-Withをつけてたらしいんだけど、CORSのpreflightを飛ばさないためにデフォルトでは削除したらしい。
https://github.com/angular/angular.js/issues/1004

X-Requested-Withをつけたい場合は以下のようにする。

myModule.config(function($httpProvider) {
  $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});

このヘッダでXHRかどうか判断する場合がある(Railsとかも)ので注意。

AngularJSのdeferredで並列実行

jQueryの$.whenみたいに複数のdeferredを同時に処理するみたいのはAngularJSだと$q.allでできる。

var d1 = $q.defer();
var d2 = $q.defer();
var d3 = $q.defer();
$timeout(function() {
  console.log('d1');
  d1.resolve('d1');
}, 10);
$timeout(function() {
  console.log('d2');
  d2.resolve('d2');
}, 1000);
$timeout(function() {
  console.log('d3');
  d3.resolve('d3');
}, 200);

$q.all([ d1.promise, d2.promise, d3.promise ]).then(function(result) {
  console.log(result); //=> ['d1', 'd2', 'd3']
});

引数は配列でもオブジェクトでもいける。

$q.whenというのもあるけどこれはオブジェクトを$qのdeferredにラップするみたいな感じっぽいので並列処理ができるやつではない。

AngularJSのControllerをネストしたときのスコープ

<div ng-controller="ParentCtrl">
  <div ng-show="isShow">foo</div>

  <div ng-controller="ChildCtrl">
    <button ng-click="toggle()">click</button>
  </div>
</div>

こういうHTMLがあったときに、ChildCtrlから$scope.isShowを操作しても反映されない。

これだとダメ。

var app = angular.module('app', []);

app.controller('ParentCtrl', function($scope) {
  $scope.isShow = false;
});

app.controller('ChildCtrl', function($scope) {
  $scope.toggle = function() {
    $scope.isShow = !$scope.isShow;
  };
});

$scope.$parent.isShowの値を更新ればいける。

app.controller('ParentCtrl', function($scope) {
  $scope.isShow = false;
});

app.controller('ChildCtrl', function($scope) {
  $scope.toggle = function() {
    $scope.$parent.isShow = !$scope.isShow;
  };
});

どうやら

$scope.__proto__ === $scope.$parent

となっているようで、文字通り親のコントローラーを継承しているようになってるみたい。

なのでChildCtrlで$scope.isShowの値を参照しようとすると親のコントローラーの値を参照できるんだけど、代入すると自分自身のプロパティとして新しくつくるんで親には影響しない、ということかな。

なので、以下のように更新する値をオブジェクトにするといける。

var app = angular.module('app', []);

app.controller('ParentCtrl', function($scope) {
  $scope.data = { isShow: false };
});

app.controller('ChildCtrl', function($scope) {
  $scope.toggle = function() {
    $scope.data.isShow = !$scope.data.isShow;
  };
});

これは$scope.dataを新しくつくるのでなく$scope.__proto__.dataを更新するからだね。

AngularJSでimgのsrcにバインドするときに404になる

<img src="{{image}}">
app.controller('MainCtrl', function($scope) {
  $scope.image = 'path/to/image';
});

とかする場合、読み込み時に {{image}} にリクエストが飛んで404になる。

ng-srcを使うといいらしい。

<img ng-src="{{image}}">

rubyの正規表現の後方参照

Ruby 1.9以降では正規表現のキャプチャに名前がつけられるのでこういう書き方ができる。

img = 'foo.png'
img_on = img.sub /\.(?<ext>\w+)$/, '_on.\k<ext>'
p img_on #=> foo_on.png

(?...) でキャプチャして \k で参照できる。$1 とか \1 みたいな意味不明な記号使わないで済むのでわかりやすくなる。

hub pull-requestでブランチを指定する

これ見て試してみたら、devブランチに対してpull requestしたいのにmasterブランチに対してしてしまって悲惨なことになった。(そしてこの操作は戻せないという・・)
http://qiita.com/kyanny/items/170a188a87925f81ae86

デフォルトはmasterだから -b オプションで指定してねってことらしい。
https://github.com/github/hub/issues/154

$ hub pull-request -i 100 -b dev

でいけた。