I am building an Ember component and it's being passed a model
{{ratings-summary-comp model=model}}
So you can call this model inside an action on the component's js file, like this:
this.get('model')
When I log this in the browser console, it outputs some kind of class:
Class {id: "1", store: Class, container: Container, _internalModel: InternalModel, currentState: Object…}
I want to convert this to a normal JS-object consisting of the model's attributes, so I can call the attributes like this:
model["attribute"]
This is so I can push a whitelist of attributes to an arrary inside the action.
How?
The model is just another Ember Object. You access its properties with get, as in
this.get('model').get(attribute)
And in the case of multiple attributes you can use getProperties:
this.get('model').getProperties('firstAttribute', 'secondAttribute', ....);
If you just need an array of attribute names, you can use Object.keys as suggested in this post on the ember github issues pages.
Running Object.keys(this.get("model")) will give you an array of attribute names.
Here's a JSBin example
Edit - The post uses Ember.keys, but that is now deprecated in favor of Object.keys.
Related
I've configured an aggregation for my Component. It looks like this:
aggregations : {
busyDialog : {
type: "sap.m.BusyDialog",
multiple: false
}
}
So, the aggregation is called "busyDialog" and can contain objects of the type "sap.m.BusyDialog".
I'm also able to get the object with its settings via my.ui5.namespace.Component.getMetadata().getAggregations().busyDialog
However, I'm not sure what's the best way to add an item to it or access an already added control in the aggregation. Are there any methods like "addbusyDialog" or something?
Was following this:
http://help.sap.com/saphelp_hanaplatform/helpdata/en/01/87ea5e2eff4166b0453b9dcc8fc64f/content.htm?fullscreen=true
OpenUI5 automatically generates the following methods for aggregations where multiple is false (where item is the name of the aggregation):
setItem(oItem)
getItem()
destroyItem()
And it creates these methods where multiple is true:
addItem(oItem)
insertItem(oItem, iIndex)
getItems()
indexOfItem(oItem)
removeItem(vItem) // item or index of item
removeAllItems()
destroyItems()
To answer your specific question, the best way to manipulate your busyDialog aggregation is to use these generated methods:
myComponent.setBusyDialog(oBusyDialog);
myComponent.getBusyDialog();
myComponent.destroyBusyDialog();
Source: https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.base.ManagedObject.html
I try to access the value of my json objects in sap ui5, but the getproperty function cannot access the required data.
But, at first, I have created a xsodata file with some service definitions, e.g.
"CUSTOMER_ATTR_G3" as "Customers";
Then I try to get these data in view.js file with the following code:
oModel.loadData("UserInterface_G3/SERVICES/CUSTOMER_ATTR_G3.xsodata/Customers?$select=CUSTOMER_ID,CUSTOMER_DESCRIPTION&$format=json");
When I am using console.log(oModel) I see in the odata section that the values are in the object but I cannot access to them. The following screenshot should show the structure of the object:
I tried for instance:
console.log(oModel.getProperty('/CUSTOMER_DESCRIPTION'));
or
console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));
But I cannot access the values of the object.
Does anybody have an idea on that?
console.log(oModel.getProperty('results/CUSTOMER_DESCRIPTION'));
You need to access your Property like that:
console.log(oModel.getProperty('d/results/0/CUSTOMER_DESCRIPTION'));
You forgot the position inside of your Array. Your Path needs the position, so if you want to get the first entry CS_0001 then you have to write result/0/CUSTOMER_DESCRIPTION.
EDIT:
Actually it depends on your Model, how you have to access the Property. Can you pls show me how you defined your oModel?
I am trying to retrieve the underlying model object from a controller so that it can be persisted (I am not using ember-data). The obvious way would simply be:
controller.get('content');
But this doesn't work. The problem can be summed up as follows:
controller.set("content", model);
sets the content as expected and at this point
controller.get('content');
works as expected. But if I then decorate the controller with other properties eg.
controller.set('IamNotPartOfTheModel', false);
then suddenly the 'content' includes this new property. I would've expected the content to remain unchanged and the new property to only be applied to the controller itself. I understand the controller is a proxy for the model so for the most part they are treated as one and the same but surely they should still be separable when needed? The whole point of this pattern is to separate data that should be stored from data that is just temporary. Am I missing something?
To have your display specific properties out of the model, just specify them explicitly in the controller... Otherwise the controller acts as a proxy for its model... just have the property "iamNotPartOfTheModel" in your controller
App.IndexController = Ember.ObjectController.extend({
iamNotPartOfTheModel: null
})
Sample fiddle here
Your controller needs to interface to some kind of model. You can't separate the persisted model from the controller except by some kind of object reference. Even if you don't use ember-data, you'll still need to create objects which then plug into the content of the controller. Have a look at Evil Trout's blog for an implementation of ember without ember-data. Its a great starting point.
I have some route like /ads/:ad_id and from my controller I can do
this.transitionToRoute('ads.ad', adObj)
How can I do the similar thing but this time passing the ID instead of the loaded object?
O course I understand that I can load an obj by ID first, but Ember's power is in doing lost of boilerplate for us.
Update: So, as by default Ember serializes the model to URL params by doing like
mode_instance -> { model_name_id: model_instance.id }
My trivial attempt was doing
this.transitionToRoute('ads.ad', { id: adObjId })
But when passed a model object Ember does not re-fetch it.
So, the question: I have a route (single ad view) that depends on ad ID. I have this ID as number. I want to transition to this route like if I simply entered the url /ads/ID
This can be accomplish by passing the URL to transitionTo. For example,
this.transitionToRoute('/ads/' + adObjId)
The model() method will be called with the params from the URL.
Here is a use case for this:
Transitioning from a list view to a detail view. In the list view, the records don't have any relations tied to them, but the detailed view should side-load relational data. For this reason, the models are not 1:1 between the list view and detailed view. There should be a way to transition simply using the id.
Cp
What's your use case for this? Most cases when you would want to specify an object by id, you already have the object to pass to transitionTo. Can you provide more context about what you're trying to do? I think you can probably accomplish it without using the object id.
In any case, I don't think there's a good way to do this, because when you transition via transitionTo(someRoute, someModel), the route's model hook is not called, and the model you pass in (someModel) is supplied directly to the other route hooks (setupController(controller, model), redirect(model), renderTemplate(controller, model)).
See Ember.JS Route api -- model method for more details.
I'd like to manage all hash state attributes (#) in one Backbone model.
StateModel //Pseudo
attributes
layout : string
modelType1 : model
modelType2 : model
This way I could consistently update history entries just by serializing this single model.
HistoryController
StateModel.bind("change", this.updateHistory);
[...]
state = StateModel.toJSON()
[...]
appRouter.navigate('v1' + state, false);
How do I make the HistoryController trigger change when the nested models (in the StateController) change?
And if the hash changes - and I'd like to update my StateModel - how do these changes propagate down to the nested models? (without causing a feedback loop)
Nested models in Backbone can be tricky because the getter and setter methods do not have built-in functionality to operate at depth. However, I have found that the best way to handle this is to store Backbone models inside other Backbone models. In your example, you would instantiate StateModel and then set its modelType1 and 2 to be, say, TypeModel instances. You can then stateModel.get("modelType1").bind("change",this.updateHistory) and stateModel.get("modelType2").bind("change",this.updateHistory). Alternatively, if you are going to be creating a lot of TypeModels, you can put this binding in the initializer function.
Secondly, you can stateModel.get("modelType1").bind("change",stateModelInstance.updateFoo) or whatever method you would like to call when the modelType model changes.
The nice thing about this pattern is that if you need stateModel to change one of the modelType models you can do stateModel.set({modelType1:newModel3}) or something of that ilk. If you have set the binding action in the ininitializer of TypeModel, everything will stay synced up. If you don't want to blow out the nested model on a change, just do stateModel.get("nestedModel1").set({"foo"}:"bar"). This shouldn't cause a feedback loop unless you have bound something to your stateModel change action that changes the nested Model again but I don't know why you would do that.