Skip to content

Instantly share code, notes, and snippets.

@whisher
Last active December 21, 2015 01:59
Show Gist options
  • Save whisher/6231714 to your computer and use it in GitHub Desktop.
Save whisher/6231714 to your computer and use it in GitHub Desktop.
/// <reference path="../lib/angular.d.ts"/>
/// <reference path="../lib/angular-resource.d.ts"/>
class MyApp{
public app:AngularModule;
constructor(){
this.app = angular.module('myApp', ['ngResource']);
this.app.config(($routeProvider:ng.RouteProvider) => {
$routeProvider.
when('/', {
templateUrl: './view.html',
resolve: {
albums: function(Album) {
console.log(Album);
return Album.getResource().query();
}
},
controller: 'AlbumController',
})
} );
this.app.value('albums',albums);
this.app.service ('Album',Album);
}
}
class AlbumController {
constructor (private $scope,private albums) {
this.$scope.test = '20 whatsomething';
this.$scope.albums = albums;
}
}
class Resource {
constructor (public $resource) {
}
setResource(params:IParams){
this.$resource = this.$resource(
params.path,
params.params,
params.methods
);
}
getResource(){
return this.$resource;
}
}
class Album extends Resource {
public params:IParams = {
path: './albums.json',
params: {
id: '@id',
collection: '@collection'
},
methods: {
query:{
method: 'GET',
isArray: false
}
}
};
constructor (public $resource) {
super($resource);
this.setResource(this.params);
}
}
interface IParams {
path:string;
params?:any;
methods?:any;
}
new MyApp();
//FLat
<!DOCTYPE html>
<html>
<head>
<title>My test TS</title>
</head>
<body>
<div data-ng-app="myApp">
<div data-ng-controller="AlbumCtrl">
<div data-ng-view></div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="./angular-resource.min.js"></script>
<script>
var services = angular.module('guthub.services', ['ngResource']);
services.factory('Album', function($resource) {
return $resource('./albums.json');
});
services.factory('MultiAlbumLoader', function(Album, $q) {
return function() {
var delay = $q.defer();
Album.query(function(recipes) {
delay.resolve(recipes);
}, function() {
delay.reject('Unable to fetch recipes');
});
return delay.promise;
};
});
var app = angular.module('myApp', ['guthub.services']);
app.config(function($routeProvider) {
$routeProvider.
when('/', {
controller: 'AlbumCtrl',
resolve: {
albums: function(MultiAlbumLoader) {
return MultiAlbumLoader();
}
},
templateUrl:'./view.html'
});
});
app.controller('AlbumCtrl',function($scope, albums) {
$scope.recipes = albums;
});
</script>
</body>
</html>
//Without http request
<!DOCTYPE html>
<html>
<head>
<title>My test TS</title>
</head>
<body>
<div data-ng-app="myApp">
<div data-ng-view></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="./angular-resource.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.factory('Devs', function() {
var data = [{name:'Joe'},{name:'Whisher'}];
return data;
});
app.config(function($routeProvider) {
$routeProvider.
when('/', {
controller: 'OneCtrl',
resolve: {
devs: function(Devs) {
return Devs;
}
},
templateUrl:'./view.html'
}).
when('/one', {
controller: 'OneCtrl',
resolve: {
devs: function(Devs) {
return Devs;
}
},
templateUrl:'./view.html'
}).
when('/two', {
controller: 'TwoCtrl',
resolve: {
devs: function(Devs) {
return Devs;
}
},
templateUrl:'./view.html'
})
.otherwise({redirectTo:'/'});;
});
app.controller('OneCtrl',function($scope, devs) {
$scope.dev = devs[0];
});
app.controller('TwoCtrl',function($scope, devs) {
$scope.dev = devs[1];
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment