How do you programmatically reload a jquery datatable? - datatables

I have a jquery datatable on a page that I want to reload after a user action. Also, I do not set the ajax parameter on my datatable because the url is always changing and I need to process the JSON data a bit after I get it.
Here is my code to initially set the datatable.
$.getJSON(uri, function (people) {
// ... code removed for brevity ...
$("#people-table").dataTable({
"data": people,
"columns": [{
"data": "id",
"title": "Id"
}, {
"data": "full_name",
"title": "Name"
}, {
"data": "phone",
"title": "Home Phone"
}, {
"data": "cell_phone",
"title": "Mobile"
}, {
"data": "email",
"title": "Email"
}]
});
How do I reload the table after a user action (like a button click)?

Here is how you do it.
(I would recommend only doing this if you must pre-process the JSON data before setting it into the datatable. If you can use the datatable ajax parameter, that is preferable.)
$.getJSON(uri, function(people) {
// ... code removed for brevity ...
if (if ($.fn.DataTable.isDataTable("#people-table"))) {
var api = $("#people-table").dataTable().api();
api.clear();
people.forEach(function(row, idx) {
api.row.add(row);
});
api.draw();
} else {
$("#people-table").dataTable({
"data": people,
"columns": [
{ "data": "id", "title": "Id" },
{ "data": "full_name", "title": "Name" },
{ "data": "phone", "title": "Home Phone" },
{ "data": "cell_phone", "title": "Mobile" },
{ "data": "email", "title": "Email" }
]
});
}

Related

Certain relations are added to the product entity for no reason

I have been using OroCommerce's API for creating products through POST requests. The request works fine but I have one problem once the entity has been created. For some reason, OroCommerce will add extra relations to the product entity.
For example, if I create a product with the name "testt", the request will have the following data :
{
"data": {
"type": "products",
"attributes": {
...
},
"relationships": {
"data": [
{
"type": "productnames",
"id": "name"
}
]
}
},
"included": [
{
"type": "productnames",
"id": "name",
"attributes": {
"fallback": null,
"string": "testt"
},
"relationships": {
"localization": {
"data": null
}
}
},
...
]
}
But when I send the POST request, the resulting entity I get has 2 name relationships instead of one, here's what the data of the resulting entity looks like with a GET request and including names :
"included": [
{
"type": "productnames",
"id": "450",
"attributes": {
"string": null,
"fallback": "system"
},
"relationships": {
"product": {
"data": {
"type": "products",
"id": "445"
}
},
"localization": {
"data": {
"type": "localizations",
"id": "1"
}
}
}
},
{
"type": "productnames",
"id": "451",
"attributes": {
"string": "testt",
"fallback": null
},
"relationships": {
"product": {
"data": {
"type": "products",
"id": "445"
}
},
"localization": {
"data": null
}
}
},
...
]
As you can see, the resulting object has an extra name relationship, even though it was never in the initial POST request to start with. I have the same issue with descriptions, shortDescriptions, where a relation with empty data gets created even if I explicitly set the data to null in the POST request.
Is there a way to prevent this?
The case is that every localizable field MUST have a value for every localization and one by default.
Such entities as
productnames
productdescriptions
productshortdescriptions
localizedfallbackvalues
can contain either a string value or fallback link to the default value or some other localization.
As is seen from your examples you have only one localization so when you create a product with
{
"type": "productnames",
"id": "name",
"attributes": {
"fallback": null,
"string": "testt"
},
"relationships": {
"localization": {
"data": null
}
}
}
the system creates a "productnames" instance with a string value inside as a default value
{
"type": "productnames",
"id": "451",
"attributes": {
"string": "testt",
"fallback": null
},
"relationships": {
"product": {
"data": {
"type": "products",
"id": "445"
}
},
"localization": {
"data": null
}
}
}
and then automatically adds "productnames" instances for each localization with fallback links (in your case one instance)
{
"type": "productnames",
"id": "450",
"attributes": {
"string": null,
"fallback": "system"
},
"relationships": {
"product": {
"data": {
"type": "products",
"id": "445"
}
},
"localization": {
"data": {
"type": "localizations",
"id": "1"
}
}
}
}
An absence of localizable field default value in a POST body will trigger "Not Null constraint".
So as detailed above every localizable field MUST has a value (or fallback) for each localization. So every time a product or localization entity is created system adds a linking entity for every intersection of every localizable field and every localization.

How to update the template of a widget in BigCommerce

I've built a widget using the content/widget-templates api endpoint using this data as the body in the request
{
"name": "Test Widget One",
"template": "<h1>Test Widget One</h1>",
"schema": [
{
"type": "tab",
"label": "Content",
"sections": []
}
]
}
What do I do if I want to edit that template? If I repost this request with a new template it will create a new widget with the same name. I've taken the provided template uuid and formed a request like so
{
"name": "Test Widget One",
"widget_configuration": {
"images": [
{
"image_url": "{{where-the-image-should-link-to}}",
"image_source": "https://cdn11.bigcommerce.com/s-n0i50vy/images/stencil/1280x1280/products/91/309/thekinfolktablecover_1024x1024__80715.1456436719.jpg?c=2&imbypass=on"
},
{
"image_url": "{{where-the-image-should-link-to}}",
"image_source": "https://cdn11.bigcommerce.com/s-n0i50vy/images/stencil/1280x1280/products/109/361/kinfolkessentialissue_1024x1024__22507.1456436715.jpg?c=2&imbypass=on"
},
{
"image_url": "{{where-the-image-should-link-to}}",
"image_source": "https://cdn11.bigcommerce.com/s-n0i50vy/images/stencil/500x659/products/85/282/livingwithplants_grande__26452.1456436666.jpg?c=2&imbypass=on"
}
]
},
"widget_template": {
"uuid": "my-uuid",
"template": "<h1>This is an update to test widget One</h1>"
},
"widget_template_uuid": "my-uuid"
}
The response reports no errors, but no update is made to the actual template of the widget.
This is the response I get back: (I replaced the uuid's with just 'uuid' for the example's sake)
{
"data": {
"uuid": "uuid",
"name": "Test Widget One",
"widget_configuration": {
"images": [
{
"image_url": "{{where-the-image-should-link-to}}",
"image_source": "https://cdn11.bigcommerce.com/s-n0i50vy/images/stencil/1280x1280/products/91/309/thekinfolktablecover_1024x1024__80715.1456436719.jpg?c=2&imbypass=on"
},
{
"image_url": "{{where-the-image-should-link-to}}",
"image_source": "https://cdn11.bigcommerce.com/s-n0i50vy/images/stencil/1280x1280/products/109/361/kinfolkessentialissue_1024x1024__22507.1456436715.jpg?c=2&imbypass=on"
},
{
"image_url": "{{where-the-image-should-link-to}}",
"image_source": "https://cdn11.bigcommerce.com/s-n0i50vy/images/stencil/500x659/products/85/282/livingwithplants_grande__26452.1456436666.jpg?c=2&imbypass=on"
}
],
"_": {
"id": "id"
}
},
"widget_template": {
"uuid": "uuid",
"name": "Test Widget One",
"schema": [
{
"type": "tab",
"label": "Content",
"sections": []
}
],
"template": "<h1>Test Widget One</h1>",
"date_created": "2020-08-05T17:46:37.802Z",
"date_modified": "2020-08-05T17:46:37.818Z",
"kind": "custom",
"storefront_api_query": "",
"icon_name": "default",
"template_engine": "handlebars_v3",
"client_rerender": false,
"site_id": 1000,
"current_version_uuid": "uuid",
"channel_id": 1
},
"date_created": "2020-08-05T18:04:24.512Z",
"date_modified": "2020-08-05T18:04:24.512Z",
"description": "",
"storefront_api_query_params": {},
"site_id": 1000,
"version_uuid": "uuid",
"channel_id": 1
},
"meta": {}
}
What do I need to do to make an update to the template of a custom widget that has already been uploaded?
You can Delete or Update an existing template using the correct API call and HTTP "method".
For example you can do a HTTP DELETE or a PUT (POST is what you seem to be doing which is adding a new template). Ensure you set the correct uuid in the path below to update or delete it:
https://api.bigcommerce.com/stores/{{$$.env.store_hash}}/v3/content/widget-templates/:uuid

Backand - use own db row id as primary key

Is it possible to specify your own row ID in backand objects? I want to form one-to-one relationsships in database, where I have two objects: userPrivateData and userProfile
I would like to use the id of Backands own internal user object as primary key for both objects. So basically a userId. The reason for this: With REST api you can access single items with GET /1/objects/{name}/{id}
Since I don't know the row id I want to use another unique identiefier as primary key, that I know - my user id.
Is this possible? Maybe I have the concept all wrong, in their docs they talk a lot about one-to-many relationships and stuff, but not about one-to-one relationships.
I think your JSON model would look something like this.
[
{
"name": "items",
"fields": {
"name": {
"type": "string"
},
"description": {
"type": "text"
},
"user": {
"object": "users"
}
}
},
{
"name": "users",
"fields": {
"items": {
"collection": "items",
"via": "user"
},
"allofuserprivatedata": {
"collection": "userPrivateDataThings",
"via": "userId"
},
"email": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"userProfile": {
"object": "userProfile"
}
}
},
{
"name": "userProfile",
"fields": {
"userid": {
"collection": "users",
"via": "userProfile"
},
"fieldOne": {
"type": "string"
},
"fieldTwo": {
"type": "boolean"
}
}
},
{
"name": "userPrivateDataThings",
"fields": {
"field1": {
"type": "string"
},
"field2": {
"type": "float"
},
"userId": {
"object": "users"
}
}
}
]
Use a collection when you want 1 to many.
Use an object when you want 1 to 1.
When you do a GET on user with deep:true the nested relationship data will come back in the response.

how do I observe for changes on the root object of dstore?

My data store consists of an array of items, and I would like to be notified when an item is added or removed from the array. How can i do this? I tried to extract a property object on the entire data, but it was unsuccessful.
<script>
require(
[
'dojo/_base/declare',
'dstore/Memory',
'dmodel/extensions/jsonSchema',
'dmodel/validators/StringValidator',
'dmodel/store/Validating',
"dmodel/Model",
"dojox/json/schema",
"dojo/text!app/model/testing/baseSchema.json",
],
function (declare, Memory, jsonSchema, StringValidator, Validating, Model, DJS, mySchema) {
var validatingMemory2 = (declare([Memory, Validating]))({
Model: jsonSchema(
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Form Elements",
"type": "array",
"items": {
"title": "Form Element",
"type": "object",
"properties": {
"id": {
"description": "identifier",
"type": "string"
},
"positionX": {
"type": "number"
},
"positionY": {
"type": "number"
},
"moduleType": {
"description": "type",
"type": "string"
}
},
"required": ["id"],
"additionalProperties": false
}
}
),
idProperty: "id",
});
validatingMemory2.setData([{ "id": "one", "positionX": 100, "positionY": 200, "moduleType": "label" }]);
console.log(validatingMemory2);
//error: property is not a function
var prpertyObject = validatingMemory2..property("items");
//I want to be alerted here when the item is added
validatingMemory2.addSync({ "id": "two", "positionX": 300, "positionY": 400, "moduleType": "label" });
console.log(validatingMemory2);
});
</script>
You should use Data Notifications in dstore.
You can monitor data changes receiving notifications using listeners that can be registered through the on() method, with different forms of notifications designed by distinct event types: add, update, and delete.
Try using this in your code:
validatingMemory2 .on('delete, add, update', function(event){
// execute when a change is made to your store
});
Additional information can be found here:
https://www.sitepen.com/blog/2014/11/17/introducing-dstore/
http://dstorejs.io/tutorials/realtime_stores.html

Ember Data JSON-API hasMany: How?

I'm familiar with the old ember-data "sideloading" model, which would look like this:
```
{
authors:[
{id:1, name:"Ernest", type: 'author', books: [1,2]
],
books: [
{id:1, name: "For whom the bell tolls", type: 'book', author:1},
{id:2, name: "Farewell To Arms", type: 'book', author:1}
]
}
But the new JSON-API method is different.
For one thing, (and I like this), attributes are separated from the id and type information, preventing namespace collisions.
I don't yet understand how to do a hasMany relationship with the JSON-API format. Can anyone point me to a doc or article on how this is expected? The examples on the JSON-API page show individual relationships, but not hasMany.
If you could write the above example in the new format, you'd have answered my question.
I found the answer in The JSON-API spec.
Each model should have a relationships key, whose value is an object with a key for each named relationship, which also has a data key that can be either a single object or an array for a hasMany relationship.
By providing an included key as top-level member, I can lazy load the entities.
In this case, the above example would be:
{
"data": [
{
"id": 1,
"type": "author",
"attributes": {
"name": "Ernest"
},
"relationships": {
"books": {
"data": [
{
"id": "1",
"type": "book"
},
{
"id": "2",
"type": "book"
}
]
}
}
}
],
"included": [
{
"id": 1,
"type": "book",
"attributes": {
"name": "For Whom the Bell Tolls"
},
"relationships": {
"author": {
"data": {
"id": 1,
"type": "author"
}
}
}
},
{
"id": 2,
"type": "book",
"attributes": {
"name": "Farewell to Arms"
},
"relationships": {
"author": {
"data": {
"id": 1,
"type": "author"
}
}
}
}
]
}
FYI: If you want to fetch a hashMany relationship at a later time, you have two other options.
1. Use a Related Resource Link:
As soon as the hashMany relationship is needed, Ember Data will call the backend with related link to fetch all the relations.
{
"data": [{
...,
"relationships": {
"books": {
"links" {
"related": "http://example.com/books"
}
}
}
}]
}
2. Use find-ids
As soon as the hashMany relationship is needed, Ember Data will call the backend with an url to fetch given ids. In this example it would be http://example.com/books?ids=1&ids=2
{
"data": [{
...,
"relationships": {
"books": {
"books": {
"data" [{
"id": "1",
"type": "book"
}, {
"id": "2",
"type": "book"
}]
}
}
}
}]
}