e.g. I have a model in Backbone.js Content and collection of this model Contents.
the model has attribute
{ id:1 , name:'rahul ',age: 27 }
now if i want to update the model with id=1 in my collection then what will be the code ?
e.g i want to update name from rahul to mehta .
Content = Backbone.Model.extend({
initialize: function() {
}
});
Contents = Backbone.Collection.extend({
model : Content,
initialize: function(models, args) {
console.log('in contents'+this.length);
}
});
what will be the code for this .?
How i will get the model from my collection of models ?
{id:1,name:mehta ,age : 27 }
and then i need to update the content of that ?should i need to change the complete data to the model or part of data only i need to update ?
For your first question:
var rahul = contents.get(1);
rahul.set({name: "mehta"});
Second question: this within initialize would be the collection.
Last question: you update what you want to update on the model using the set function of the model.
Please read the documentation: http://documentcloud.github.com/backbone/
Related
Using Laravel I am some data that follow the same model. I would like to duplicate.
I tryed this code :
public function duplicate($id)
{
$vpn = Vpns::findOrFail($id);
$newVPN = $vpn->replicate();
$newVPN->save();
return redirect()->route('home');
}
Problem is just date some datas are duplicate but not 100%. I have a new entry but 80% of data of precedent model are duplicated.
Example : text data are duplicated but not checkbox values or something like that.
DO you have some ideas ?
Thanks
Please Try This
public function duplicate($id)
{
$vpn = Vpns::find($id);
$newVPN = $vpn->replicate();
$newVPN->save();
return redirect()->route('home');
}
I have two models customer and orders. They are already fecthed separately
$customers = customer::all();
$orders = orders::all();
customerID=1 has orderID : 1, 2,4 customerID=2 has orderID : 3,5,9
They are related (hasMany, belongsTo) but the problem is inside my for a certain reason they are separated but I want to send them as response in API using toJson or ToArray as one data having the orders nested to their correct customers.
How can I achieve that linking to have at the end one variable $customersWithOrders that should be transformed to JSON ?
I am using laravel 5.5
I don't know what the context is. Defining relationships as other answers mentioned is a good solution.
In addition, I recently read a pretty good article about this specific scenario.
So you can also do something like this, if you have already retrieved customers and orders:
$customers = Customer::all();
$orders = Order::all();
return $customers->each(function ($customers) use ($orders) {
$customer->setRelation('orders', $orders->where('customer_id', $customer->id));
});
If you already have a relation you just use it. For example, in model Customer.php:
public function orders()
{
return $this->hasMany(Order::class);
}
Then you'd get customer orders by calling $customer->orders
If you already have defined relations, you can simply fetch data with eager loading
// in customer model
public function orders()
{
return $this->hasMany(orders::class, 'orderID');
}
// in controller
$customersWithOrders = customer::with('orders')->get();
return response()->json(['customersWithOrders' => $customersWithOrders]);
// in js
for (let customer in response.customersWithOrders){
let orders = customer.orders
}
I have setup a route like this:
Route::get('/prod/{id}/{title?}', 'Web\GetItemController#getItem')->where('id', '[0-9]+')->name('prod.item');
This is to retrieve a item based on the id the title is only a optional parameter in order to make the url look somewhat nicer.
Then in the controller I fetch the item like this:
class GetItemController extends Controller
{
public function getItem($id, $title = null)
{
$item = new product();
$data = $item->getProdInfo($id);
// Show view with data...
}
}
So I should be able to fetch the item with or without the title parameter.
So a call like this would trigger the route "https://www.x.com/prod/3/sonic-free-games"
But is there any difference in performance using slug like I do above?
/prod/{id}/{title?} vs /prod/{id}
This question already has answers here:
How do I insert a model into a backbone.js collection in a specific index?
(3 answers)
Closed 6 years ago.
I have a collection of models. When i update one of the model i need to to set it in the collection.
I could get the oldModel and it's index but how can i set the returned one (model) into the collection in the index desired.
I need something like this regionProofsView.collection.models.set(index,model) .
This is my code:
myView.on("childview:model:update", function(childView, id) {
var fetchingModel = VialinkSignManager.request("model:update", id);
$.when(fetchingModel).done(function(model) {
var oldModel=myView.collection.get(id);
var index=regionProofsView.collection.models.indexOf(oldProof);
// here i need to set the model into the collection
myView.render();
});
});
You just want to replace the old model with the new model right? Backbone already tries to take care of this for you. Assuming you haven't changed the id, and the old model id = new model id, then all you have to do is call collection.set(model). It will search the collection for a model with the same id and from http://backbonejs.org/#Collection-set
if the model is already in the collection its attributes will be merged;
After research in the backbone documentation I've find this method as a solution.
After getting the oldModel and it's index I remove it and add in its index the new model:
myView.on("childview:model:update", function(childView, id) {
var fetchingModel = VialinkSignManager.request("model:update", id);
$.when(fetchingModel).done(function(model) {
var oldModel = myView.collection.get(id);
var index = myView.collection.models.indexOf(oldModel);
myView.collection.remove(oldModel);
myView.collection.add(model, {
at: index
});
});
Why don't you listen to the change event? The change event is triggered when the model changes.
this.listenTo(this.collection, 'change', view.doSomething);
I have 3 tables, products, taxonomies and product_taxonomy, as you can tell the 3rd table is a pivoting table. In taxonomies table, I hold a field called num_products which keeps track of the quantity of products that belongs to this taxonomy. Now how can I trigger a model event every time a product is attached to or detached from a taxonomy? I want to update that num_products value in the model event.
Laravel models have events that you can hook into. You have the following options:
creating
created
updating
updated
saving
saved
deleting
deleted
restoring
restored
You'd code it like this:
User::creating(function($user)
{
if ( ! $user->isValid()) return false;
});
Docs: http://laravel.com/docs/4.2/eloquent#model-events
Or you could use Model Observers that are baked in. You have the following options:
creating
updating
saving
You would write a method on your model:
class UserObserver {
public function saving($model)
{
//
}
public function saved($model)
{
//
}
}
You can then register then register the observer:
User::observe(new UserObserver);
Docs: http://laravel.com/docs/4.2/eloquent#model-observers
Hope it helps.