Skip to content

Instantly share code, notes, and snippets.

@adiehl96
Last active July 18, 2019 08:07
Show Gist options
  • Save adiehl96/925e10b056a2b6ee2ac6be298c186ebe to your computer and use it in GitHub Desktop.
Save adiehl96/925e10b056a2b6ee2ac6be298c186ebe to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import Timesheet from '../models/timesheet';
import Receipt from '../models/receipt';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
Receipt: Receipt.create(),
Timesheet: Timesheet.create(),
});
import Ember from 'ember';
Ember.ComputedProperty.prototype.saveAs = function(type, options) {
options = options || {};
options.isArray = type === "array";
var meta = {
type: type,
options: options,
isAttribute: true
};
this.meta(meta);
return this;
};
Function.prototype.saveAs = function(type, options) {
return Ember.computed({
get: this,
set: this
}).saveAs(type, options);
};
export default Ember.Object.extend({
didDefineProperty: function(proto, key, value) {
if (value && value.meta) {
var meta = value.meta(),
options = meta.options,
constructor = this.constructor;
if (meta.isRelation === 'belongsTo') {
let property = Ember.computed(key, {
get: function() {
return this.get(key + '._meta.id');
},
set: function(k, v) {
return v || null;
}
});
if (meta.foreignKeyAttribute) {
property.saveAs('int');
}
Ember.defineProperty(proto, options.foreignKey, property);
}
if (meta.isAttribute) {
Ember.addObserver(proto, key, null, 'attributeDidChange');
if (meta.options.isArray) {
Ember.addObserver(proto, key+'.[]', null, 'arrayAttributeDidChange');
}
if(this){
let lole = Ember.get(this, 'lole') || {};
Ember.set(this, 'lole', lole);
lole = Ember.get(this, 'lole');
Ember.set(lole, key, meta);
Ember.set(this, 'lole', lole);
}
constructor._attributes = constructor._attributes || {};
constructor._attributes[key] = meta;
console.log(constructor._attributes.amounts.type)
}
}
},
});
import Basic from './basic';
export default Basic.extend({
});
import Basic from './basic';
export default Basic.extend({
amount: "lel",
amounts: Ember.computed('nonce', {
get: function() {
// console.log("model timesheet attempt get")
// Timesheets always have one amount by default
return [Webapp.__container__.lookup('service:store').getModel('Amount').create({ hours: 0 })];
},
set: function(k, v) {
if (!v || v.length === 0) {
return [Webapp.__container__.lookup('service:store').getModel('Amount').create({ hours: 0 })];
} else {
let cur = this.cacheFor('amounts');
if (cur && v.length === cur.get('length')) {
cur.forEach(function(amount, i) {
amount.replaceUpdate(v[i]);
});
return cur;
}
let dosn = v.map(function(fields) {
return Webapp.__container__.lookup('service:store').getModel('Amount').create(fields);
});
return dosn;
}
}
}).saveAs(function(amounts) {
amount
}),
});
import Basic from './basic';
export default Basic.extend({
amount: "lol",
amounts: Ember.computed({
get: function() {
// A receipt without an amount is useless in the websystem -
// make sure it always has at least one.
return [Webapp.__container__.lookup('service:store').getModel('Amount').create()];
},
set: function(k, v) {
if (!v || v.length === 0) {
// A receipt without an amount is useless in the websystem -
// make sure it always has at least one.
return [Webapp.__container__.lookup('service:store').getModel('Amount').create()];
} else {
// HACK if amounts are set, check if they
// are the same length as the current amounts.
// If so, just override properties so that
// forms do not need to reinitialize.
let cur = this.cacheFor('amounts');
if (cur && v.length === cur.get('length')) {
cur.forEach(function(amount, i) {
amount.replaceUpdate(v[i]);
});
return cur;
}
return v.map(function(fields) {
return Webapp.__container__.lookup('service:store').getModel('Amount').create(fields);
});
}
}
}).saveAs(function(amounts) {
minutes
}),
});
import Ember from 'ember';
import Receipt from '../models/receipt';
export default Ember.Route.extend({
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
The meta of Receipt:
<br>
{{Receipt.constructor._attributes.amounts.type}}
<br>
<br>
<br>
<br>
The meta of Timesheet:
<br>
{{Timesheet.constructor._attributes.amounts.type}}
<br>
{{Receipt.constructor._attributes.amounts.type}}
{{Timesheet.constructor._attributes.amounts.type}}
<br>
<br>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment