javascript - Turning input data into an array on a backbone model? -


using mongodb, , backbone models trying store input value array inside model?

so @ barebones let's have small little form.

<form id="send-message">   <input name="message" class="message"/>   <button type="submit" class="send">send</button> </form> 

then in backbone code view contains form, submit data. using socket.io have code thats this. don't think posting full code view necessary, , prevent confusion.

var marionette = require('backbone.marionette'),     messagesview = require('./messages'),     userslistview = require('./users_list'),     socket = io.connect();  module.exports = chatview = marionette.itemview.extend({     template: require('../../templates/chat.hbs'),     events: {         'submit #send-message': 'sendmessage'     },      initialize: function() {         var self = this;         this.messagesview = new messagesview({ collection: window.app.data.messages });         this.userslistview = new userslistview({ collection: window.app.data.messages });         socket.on('new message', function(data) {             self.createmessage(data);         });     },      onrender: function() {         this.messagesview.render().$el.appendto(this.$el.find('.message-content'));         this.userslistview.render().$el.appendto(this.$el.find('.users-list'));     },      sendmessage: function(e) {         e.preventdefault();          var $message = this.$el.find('input.message');         socket.emit('send message', $message.val());          $message.val('');     },      createmessage: function(data) {           var model = window.app.data.messages.where({username: data.username});          _.each(model, function(model) {             var values = {                 message: data.message             }             model.save(values);         });          window.app.core.vent.trigger('app:log', 'add view: created new message!');     }  }); 

so summed submits input data node server server emits response , triggers createmessage.

my question want capture these messages , store them array inside model. data structure kind of this.

// represents ideal structure of model @ barebones    var user = {     username: 'grant',     message: {message1: "hey guys", message2: "michigan state win tourney"} } 

lets take closer @ createmessage method.. see save message below, unsure how messages saved on model append new message, creating array. i'll leave now, have tried using .push() function , tried various ways failing...

createmessage: function(data) {       var model = window.app.data.messages.where({username: data.username});      _.each(model, function(model) {         var values = {             message: data.message         }         model.save(values);     });      window.app.core.vent.trigger('app:log', 'add view: created new message!'); } 

the data goes mongodb have controller handles , (i think) overrides backbone.sync, controller fired router when put request made app.put('/api/messages/:id', messages.update); update method handles data, possibly create array here.

update: function(req, res) {     models.message.update({ _id: req.params.id }, {message: req.body.message //somewhere here append?}, function(err, message) {         if (err) {           res.json({error: 'update failed.'});         } else {           res.json(message);         }     }); } 

edit: thinking it, want generate array of objects.. biggest problem figuring out how append new object , generate new key?

for example

var user = {     username: 'grant',     message: {message1: "hey guys"} } 

a user submits new message, how create object fresh key, tried using backbone length of objects, got kind of hairy since vanilla based now..

what you're trying make object, not array - that's why .push() doesn't work.


function(value) {     user.message['message' + (object.keys(user.message).length + 1)] = value; } 

this add new value object, key 'message' + amount of old 'messages'.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -