ruby on rails 4 - adding models to backbone collections -


i have rails app 4 model classes, multiple instances in each table. have created backbone model , collection classes in coffeescript match. can load of collections , can render them in views. far, good. here 1 of collections, associated model:

class window.coffeewater.collections.histories extends backbone.collection url: '/api/histories' model: history  class window.coffeewater.models.history extends backbone.model 

i need able create history model object, , add histories collection. documentation states have set 'collection' property when creating new model, in order 'url' property collection. problem can't seem set 'collection' property value correctly, because url property not set on model instance

attributes = {'start_time': new date (1434740259016), 'stop_time': new date (1434740259016 +(86400*1000)), 'valve_id': 2} options = { collection: window.coffeewater.collections.histories } history = new window.coffeewater.models.history(attributes, options) window.coffeewater.objects.collections.histories.add(history) 

inspecting resulting 'history' object not show same attributes exist in models in collection, , url property missing.

i @ loss. have example on how this? backbone.js docs not show relevant examples.

the way url in model defined this.

if there url property in model.

class window.coffeewater.models.history extends backbone.model url:'/api/history/1' 

then when model.fetch(), call url. if property not found, see if associated collection have 'url'. try set collection variable.

what missing this.

class window.coffeewater.collections.histories extends backbone.collection 

is definition of class. not object yet. need initialize it, in java.

var historycollection=new window.coffeewater.collections.histories() 

now have collection object. when

historycollection.add(history); 

the "collection" of model automatically set "historycollection" object, contains 'url'. in fact, not need manually put in option.

basically

attributes = {'start_time': new date (1434740259016), 'stop_time': new date (1434740259016 +(86400*1000)), 'valve_id': 2}  var historycollection=new window.coffeewater.collections.histories() var history = new window.coffeewater.models.history(attributes) historycollection.add(history) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -