I'm creating a multipage survey with Node.js 6, Express.js 4 and Sequelize 4.4.2. While the user fills out the survey several model objects are build, but not persisted, this will not happen until the survey is completely done.
Some of these models are associated with each other and I want to know if it's possible to use the .build() function of an defined model with initial values (such as "name" or "address") as well as an previously build but not persisted model object.
Maybe a simple example, what I mean:
const comp = Company.build({
Name: 'My Company',
Location: 'Ireland',
Employees: [] // Employee would be another model in this case
});
It seems, that Employees is ignored while the obejct is built. Is there a way to attach properties which are NOT defined as field for the model (in this case: Company) but as association?
Hope you got, what I mean ...
Thank you in advance! :)
Solved it by myself! I just append the built models after to the dataValues property of the previously built model object and not directly whilst the build process.
Related
Ive looked related posts and couldn't quite find what I was looking for.
So I am build a backend rest api and I have certain tests I am collecting data on. The tests have their own models, and these models are associated with collections (obviously).
So I have a separate controller for each model. Now I have a "job" controller which queries data from each separate test. Now I have a separate script where I store these model objects in an JSON object. I am wondering how I can access these models properly (I am close but cant quite assign properly). Here is the block:
const testMappings = {
'aprobe':aprobe,
'status':status,
//'rxserial':rxserial,
}
Now when I try assignment as follows, where testMappings is the imported script variable:
const testMappings = activeTests.testMappings;
console.log(testMappings['aprobe']);
I get the following output:
Model {aprobe}
I would like to access the actual aprobe object. Also if anyone knows a better way of dynamically assigning these (instead of having bunch of if statements ie if(name == 'aprobe').... do something), it would be much appreciated.
You are probably looking for something like below :
const name = 'aprobe';
Object.keys(testMappings).indexOf(name) > -1 ? testMappings[name] : null
the above should give you: Model {aprobe}
So basically if the key exists in your object then you'd like to fetch the value of that key which would give you your model dynamically.
I have an nested data structure as follows:
Job:{
JobId: 1,
NumberTrackr: 2,
Trackrs: [
1 : { TaskTrackrID: a,
NumberSlots:1,
slots: [
slot1: {uniqueId:foo, you: get, the:[point, by, now]}
]
},
2 : { TaskTrackrID: b,
NumberSlots:1,
slots: [
slot1: {uniqueId:bar, you: get, the:[point, by, now]}
]
}
]
}
And my application uses this data hierarchy (calculating the sum of a specific Trackr's 'children's' foo attribute) as well as each data level as a whole (for example, calculating statistics from the content of all slots regardless of their 'parent' TaskTracker).
Im new to Ember, but my I was thinking of creating a model for each object level (TaskTrackr, Slot etc). This model would consist of all the attributes shown above as in addition to some sort of array of sub models (using arraycontroller?). Said submodels would also have attributes as well as their own array of sub models. It is important that the highe level objects can have computed properties calculated from their sub objects. It is also important that I can access each level as a whole and modify specific attributes.
What would the code look like that would enable me to access this data both as a tree as well as on a level basis?
Thank you so much for your help
Take a look at Ember Data. It supports these types of relationships.
App.Job = DS.Model.extend({
trackers: DS.hasMany('App.Tracker')
})
App.Tracker = DS.Model.extend({
job: DS.belongsTo('App.Job');
})
// ... and so on
var job = App.Job.createRecord();
job.get('trackers').pushObject(App.Tracker.createRecord());
Just to note. You mentioned using ArrayController and you will likely use that inside of your Ember application. However, when modeling your data/relationships you will not use any controllers. You should be able to do do what you pasted above with using only DS.Model
I have an application that saves a user's search criteria in localStorage, where each saved search is represented as an instance of an Ember.js model:
Checklist.SavedSearch = DS.Model.extend({
id: DS.attr('string'),
filters: DS.attr('string')
});
When the "save" button is pressed, the controller creates a model instanced and creates a record for it:
Checklist.savedSearchController = Ember.ArrayController.create({
[..]
save: function(view) {
var saved_seach = Checklist.SavedSearch.createRecord({
id: 'abcd',
filters: '<json>'
});
Checklist.local_store.commit();
}
});
Checklist.local_store is an adapter I created (this is unsurprisingly where the problem probably begins) that has a basic interface that maps createRecord, updateRecord, etc. to a bunch of get/set methods that work with localStorage (loosely based on a github fork of ember-data). The adapter appears to work fine for some basic tests, particularly as findAll has no issues and returns values added manually to localStorage.
Here is the relevant method within Checklist.local_store:
createRecord: function(store, type, model) {
model.set('id', this.storage.generateId);
var item = model.toJSON({associations: true});
this.storage.setById(this.storage_method, type, id, item);
store.didCreateRecord(model, item);
}
The problem is that when createRecord is called by the controller, absolutely nothing occurs. Running it through the debugger, and logging to console, seems to show that the method isn't called at all. I imagine this is a misunderstanding on my part as to how Ember.js is supposed to work. I'd appreciate help on why this is happening.
I come from a ruby and php background, and have perhaps foolishly dived straight in to a JS framework, so any other comments on code style, structure and anything in general are welcome.
Ember Data doesn't change createRecord on the controller so it shouldn't behave any differently. It's possible that there was something related to this in the past, but it's certainly not the case anymore.
I am new to ST2, and I am having difficulty with trying to do something I think should be simple.
I have a model that is a user with a hasMany association called emails. I have a list that successfully loads the top level user items, and when I tap on an item in the list, I am able to access the users properties using the record passed into the tap event via
record.get('displayName');
But any way I have tried to access the collection of emails from the record object has not worked.
I've tried :
record.get('emails'); // doesn't work
record.getEmails(); // doesn't work
record.emails().each(...);
record.emails() exists, but no idea what it is. each() yields nothing, though it evaluates ok.
Can anyone explain to me how to get at the association data elements from this record object?
Here is an example of the JSON I am generating :
[
{
"id":0,
"displayName":"Display Name 0",
"emails":[
{
"value":"email#thehost.us",
"type":"home",
"pref":"true"
}
]
}
]
record.emails() is the correct way to do it. That will return an instance of Ext.data.Store which you can then load (if there is no data) the records, or loop through them.
More documentation on the hasMany association is available here: http://docs.sencha.com/touch/2-0/#!/api/Ext.data.association.HasMany
In this stackoverflow post i read about filtering backbone collections and using subsets.
One answer (by sled) recommends using backbone.subset.js (usage example).
I could not find any further resources on backbone.subset.js and I failed implementing it into my project.
It seems like backbone.subset.js is the perfect solution for what i'm trying to achieve.
(Having one "parent" collection that holds all models at all times, and depending on user input filtering the relevant models from the parent collection into a backbone.subset collection.)
My "parent" collection, holding all tasks:
var TasksAll = Backbone.Collection.extend({
url: '/tasks', // the REST url to retrieve collection data
model: Task // the models of which the collection consists of
});
var allTasks = new TasksAll();
Now i want to create a subset collection for e.g. tasks where task.status = 0:
var TasksTrash = new Backbone.Subset({
superset: allTasks,
filter: function(Task) {
return Task.isTrash();
}
});
var trashTasks = new TasksTrash();
Whereas inside the Task model, the method "isTrash" returns true if:
this.get('status') == 0
a) Are there any more resources on backbone.subset.js?
b) How do I implement above scenario?
c) Can I pass 'superset' and 'filter' options as params to the Backbone.Subset init function?
d) I looked into the backbone.subset.js code, when I 'reset' my parent Collection my subset Collections should be updated straight away, right?
PS: I'm fairly new to Backbone. Thanks for your help.
Looking at the source for backbone-subset, it looks as though there is a pre-initialization hook which you could utilize in order to make the 'sieve' or filter available as an option or argument:
https://github.com/masylum/Backbone.Subset/blob/master/backbone.subset.js#L50
As for providing parent as an argument, there is an outstanding patch to add that exact functionality:
https://github.com/masylum/Backbone.Subset/pull/5
With it, you can pass in parent as an option, if it is not an option the library will fall back to looking for it on the object Prototype