Getting nearer to mixin pattern

This commit is contained in:
Andrew
2015-03-17 21:57:29 -05:00
parent 59e5ea5fa6
commit d89fd9c8de
8 changed files with 89 additions and 20 deletions

View File

@@ -1,7 +1,18 @@
import * as Platform from '../platform';
var ILLEGAL_ASSIGN_FIELDS = {};
export class Ion {
constructor() {
var platformName = Platform.getPlatform();
var platformConfig = this.$config._platforms[platformName];
if (platformConfig) {
platformConfig._mixins.forEach(mixin => {
mixin(this);
});
}
}
assign() {
for (var i = 0, ii = arguments.length; i < ii; i++) {
var obj = arguments[i];

View File

@@ -1,19 +1,18 @@
/*
import {TabbarConfig} from '/components/tabbar/tabbar';
import {Draggable} from '/behaviors/draggable';
import {TabbarConfig} from '../../tabbar';
// import {Draggable} from '/behaviors/draggable';
TabbarConfig.platform('android')
.template(require('./android-template.html'))
.template('./android-template.html')
.mixin(function(tabbar) {
Draggable(tabbar);
tabbarInstance.setAsHeader();
// Draggable(tabbar);
// tabbarInstance.setAsHeader();
tabbar.assign({
onDragStart() {},
onDrag() {},
onDragEnd() {}
press() {
alert('pressing from android mixin');
}
});
});
<ion-tabs config-platform-android />
*/
export var a = 2;

View File

@@ -1,23 +1,31 @@
import {Component, Template} from 'angular2/angular2';
import {Inject} from 'angular2/di';
import {Ion} from '../ion';
import {IonConfigService} from '../../config';
export var TabbarConfig = IonConfigService();
@Component({
selector: 'ion-tabbar',
bind: {
title: 'view-title'
}
},
services: [TabbarConfig]
})
@Template({
inline: `<button (click)="press()">Tabbar: {{title}}</button>`
})
export class Tabbar extends Ion {
constructor() {
// Test that Ion#assign works
this.assign({
press: () => {
alert('pressed!');
}
});
constructor(
// Creates a TabbarConfig instance for this specific instance of Tabbar.
// this instance is created with a cloned version of all the globally
// set config properties.
@Inject(TabbarConfig) config
) {
debugger;
this.$config = config;
super();
}
}

41
src/config.js Normal file
View File

@@ -0,0 +1,41 @@
import * as Platform from './platform';
var i = 0;
export function IonConfigService() {
function Config() {
this._platforms = {};
for (var key in Config._platforms) {
this._platforms[key] = Config._platforms[key]._clone();
}
}
Config._platforms = {};
Config.platform = platformFn.bind(Config);
Config.prototype.platform = platformFn;
function platformFn(name) {
return this._platforms[name] || (this._platforms[name] = new SubConfig(name));
}
return Config;
}
class SubConfig {
constructor(name, mixins = [], template = '') {
this._name = name;
this._mixins = mixins;
this._template = template;
}
mixin(mixinFn) {
this._mixins.push(mixinFn);
return this;
}
template(url) {
this._template = url;
return this;
}
_clone() {
return new SubConfig(this._name, this._mixins.slice(), this._template);
}
}

View File

@@ -0,0 +1,4 @@
export function getPlatform() {
return 'android';
}