Collection, Model, Store, Source

Hi,

currently I play with the new data layer feature on ENYO 2.3.0-rc6. In my case I would like to add an entry to a collection. It works great, but I don't know the solution to call the function 'commit' inside my own enyo.Source.

Here my code:
inside the view
var filmEntry = new Lomoplog.FilmEntry();
filmEntry.set("filmname", this.$.inputTitle.getValue());
filmEntry.set("filmiso", this.$.inputIso.getValue());
filmEntry.set("filmcomment", this.$.inputComment.getValue());
this.get("controller").add(filmEntry);
app.js
kind: "enyo.Application",
components: [
	{ name: "filmCollection", kind: "Lomoplog.FilmCollection" }
],
view: "Bootplate.MainView",
[...]
initComponents: function() {
	this.inherited(arguments);
	enyo.store.addSources({mysource: "Lomoplog.Source"});
}
collection
enyo.kind({
	name: "Lomoplog.FilmCollection",
	kind: "enyo.Collection",
	model: "Lomoplog.FilmEntry",
	defaultSource: "mysource",
	store: "enyo.store"
});
model
enyo.kind({
name: "Lomoplog.FilmEntry",
kind: "enyo.Model",
attributes: {
	// moving this property to the attributes schema definition will
	// allow the onChange event to be fired when that property changes
	selected: false,
	filmname: null,
	filmiso: null,
	filmcomment: null
}
});
source

enyo.kind({
	name: "Lomoplog.Source",
	kind: "enyo.Source",
	fetch: function(rec, opts) {
        // implement code to fetch records
        //opts.success(data);        // call success callback to return data
    },
    commit: function(rec, opts) {
        // implement code to store records
        opts.success();        // call success callback
    },
    destroy: function(rec, opts) {
        // implement code to destroy records
        opts.success();        // call success callback
    },
    find: function(rec, opts) {
        // implement code to find records
        //opts.success(data);        // call success callback to return data
    }
});
Thank you in advance!
Cheers
Björn

Comments

  • enyo.Store calls commit when its commitRecord() method is called. That's called by enyo.Model when you call commit() on the model instance.
  • Hi Ben,

    thanks for your reply. After adding the following lines to my own model implementation and changing the view implementation it works very well.

    model
    defaultSource: "mysource",
    store: "enyo.store",
    view
    var filmEntry = enyo.store.createRecord("Lomoplog.FilmEntry", null, null);
    filmEntry.set("filmname", this.$.inputTitle.getValue());
    filmEntry.set("filmiso", this.$.inputIso.getValue());
    filmEntry.set("filmcomment", this.$.inputComment.getValue());
    this.get("controller").add(filmEntry);
    filmEntry.commit();
    Is the approach with enyo.store.createRecord ok or exist a better solution?

    Cheers Björn
  • If your model is part of a collection, you would use collection.createRecord() and have it manage the store. However, code not using a collection can use the store's createRecord. Here, you're using the default enyo.store instance, but if you have a custom store, you'd invoke with that instead.
  • Thank you Ben. Here my new source code:
    var filmEntry = this.get("controller").createRecord(
    	{
    		"filmname": this.$.inputTitle.getValue(),
    		"filmiso": this.$.inputIso.getValue(),
    		"filmcomment": this.$.inputComment.getValue()
    	}	
    );
    if (this.get("controller").length === 1) {
    	this.get("controller").at(0).set("selected", true);
    	this.doActivateFilm( {index: 0} );
    }
    filmEntry.commit();
    It works.
Sign In or Register to comment.