Spine.js, setting model default values - coffeescript

There is in rails we have lifecycle hooks, which allows us doing this:
class Subscription < ActiveRecord::Base
before_create :record_signup
private
def record_signup
self.signed_up_on = Date.today
end
end
Is there best way to accomplish same thing (i need it to set some default values) in Spine.js ?
Currently i doing it this way, but maybe there is better way exists ?
class Subscription extends Spine.Model
#record_signup: (self) ->
self.signed_up_on = new Date()
Subscription.bind 'beforeSave', Subscription.record_signup

CoffeeScript class bodies are executable :
class Subscription extends Spine.Model
#record_signup: (self) ->
self.signed_up_on = new Date()
#bind 'beforeSave', #record_signup

How about overriding the standard Model.create function to include your default values if they aren't set?
#create: (atts, options) ->
atts.myVal or= 'someDefault'
record = new #(atts)
record.save(options)

Related

Extend ProposalProvider in external Eclipse Project via Extension Point

I try to extend my MyDSLProposalProvider from an external Eclipse RCP Project. I created an extension point schema which requires a class property which extends my ProposalProvider. In the new project I extend the class an overrode some methods justs to give me some output so I can see that the external method is called. But this is currently not happening. Is there anything I have to consider?
Currently the hirachy looks like:
MyDSLProposalProvider extends AbstractMyDSLProposalProvider
ExternalProposalProvider extends MyDSLProposalProvider
I rewrote a Method generated in the AbstractMyDSLProposalProvider but when its triggered the predefined Method in the AbstractMyDSLProposalProvider is called and not my new implementation.
public class ExternalMyDSLProposalPovider extends MyDSLProposalProvider
{
#Override
public void completeComponent_Name(EObject model, Assignment
assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
System.err.println("extern");
if(model instanceof Component)
{
createProposal("foo", "foo", context, acceptor);
}
super.completeComponent_Name(model, assignment, context, acceptor);
}
}
This is the class in the external Eclipse Project.
Thanks for the help.
When you declare an extension point using a schema that you have defined Eclipse puts that declaration in the extension point registry. That is all that is does, you must then write code to make uses of those declarations.
You read the extension point registry using something like:
IExtensionRegistry extRegistry = Platform.getExtensionRegistry();
IExtensionPoint extPoint = extRegistry.getExtensionPoint("your extension point id");
IConfigurationElement [] elements = extPoint.getConfigurationElements();
elements is now an array of the declarations in the various plugins using the extension point.
IConfigurationElement has various methods to get the values of the attributes of the declaration.
If you have defined a class in one of the attributes you can create an instance of the class using:
IConfigurationElement element = .... a config element
Object obj = element.createExecutableExtension("attribute name");
In your case the result should be your ExternalMyDSLProposalPovider.
You will then need to hook this object up with whatever is doing to proposals.

How to access Number through separate class?

Hey everyone so this is something that I have always had trouble trying to accomplish or understand. So I have my main Engine class calledescapeEngine where I have a private var nScore I want to be able to access this variables through a separate class called mcPlanets but I don't know how I would accomplish this. I know how to do the opposite but not how to access a var from my main Engine class. Can anyone help me out?
I am not sure what you are trying to do, but here is an example that may help you:
Inside esacapeEngine class (main), create a public var nString and new instance of mcPlanets.
// two lines in escapeEngine.as
var nScore = 0;
var mcPlant = new mcPlanets(this);
So, when you create new mcPlanets, pass in the reference (keyword 'this' in the parentheses). Now mcPlanets knows about your main class.
And now in mcPlanets class, write this:
public class mcPlanets
{
private var escapeEngine;
public function mcPlanets(main) // 'this' = 'main'
{
escapeEngine = main;
// access nScore defined in main class
escapeEngine.nScore = 5;
}
}
In this example, nScore must be a public variable, it could be a private but you should use 'get and set' methods.

Can I "hook" a subscription in ngrx store

I would like to keep track of how many times a certain key is subscribed to in #ngrx/store. I don't want to have repeated code in each component that subscribes but was hoping to hook into select() somehow. I don't think #effects apply here because I am not looking at dispatched actions.
Does anyone have any ideas on how I would implement this?
Assuming that subscribing to a key means selecting something from the store.
You can try extending the Store with your own service and using that then override the select method with something like:
#Injectable()
class CountingStore<S> extends Store<S> {
public keyCount: {[key:string]: number} = {};
public select = (key: string) => {
keyCount[key] = keyCount[key] ? keyCount[key] + 1 : 1;
return super.select(key);
}
}

How to add instance of the class which satisfies import to CompositionContainer

I am facing the following problem:
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Type1).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Type2).Assembly));
using (CompositionContainer container = new CompositionContainer(catalog))
{
}
I need one more export:
Export[(typeof(Type3))]
The thing is that I can't include an assembly with the class which has this Export attribute. I want to tell the container that:
var myObject = new Type4();
myObject (the instance of Type4) should be exported each time the Import[(typeof(Type3))] is needed. Besides I can't mark Type4 with Export[(typeof(Type3))] and also I want the instance of the class to be used by MEF (so marking this class with Export attribute doesn't work, because I am changing myObject before I pass it to MEF and I want it to be used to satisfy Import).
Then when I try to do:
container.SatisfyImportsOnce(importer);
I expect that MEF will get all the objects from the assemblies in catalog, and for the missing Type3 it will use myObject. This should be the value when I do:
container.GetExportedValue<Type3>();
I spent one day trying different approaches: custom ExporterProvider and some sort of inheritance from Type4 to mark it with proper Export attribute but I can't get it working as I want.
I would be very grateful for help.
Thank you!
Ok, already found an answer.
First problem was that I added 2 the same AssemblyCatalogs to AggregateCatalog - don't do that.
The solution is to use CompositionBatch:
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Type1).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Type2).Assembly));
var myObject = new Type4();
using (CompositionContainer container = new CompositionContainer(catalog))
{
var batch = new CompositionBatch();
Export ex = CreateExport<Type3>(myObject); //Custom implementation
batch.AddExport(ex);
container.Compose(batch);
var val = container.GetExportedValue<Type3>(); //value == myObject
}
Thank you!

ChaplinJS: Referencing a CollectionView property in its template

I have a Collection in ChaplinJS that has the following initialization code:
Chaplin = require 'chaplin'
Collection = require 'models/base/collection'
Domain = require 'models/domain'
mediator = require 'mediator'
module.exports = class Domains extends Collection
model: Domain
# Initialize the SyncMachine
_(#prototype).extend Chaplin.SyncMachine
initialize: ->
super
#.totalHits = 0
How can I reference totalHits in the template of its view? I am using handlebars templates, and writing {{totalHits}} returns nothing.
Incidentally, shouldn't I be able to rewrite the above code with:
module.exports = class Domains extends Collection
model: Domain
totalHits: 0
Found the solution:
In my CollectionView I can override getTemplateData and pass into it whatever I want, including the complete collection object:
getTemplateData: ->
templateData = super
templateData.collection = #.collection
templateData
Then in the handlebars template I can do {{collection.totalHits}}