I am using AppStorage to save some user settings such as Bools, and I initialise a Bool to true in the class, but am wondering when the app restarts will it reinitialise the variable to that initial value or read the saved value?
#AppStorage("pullData") var pullData: Bool = true
In this example will the class set pullData to what is in AppStorage or to true when the app starts up again?
true in your example above is the value to fall back on if there is nothing in user defaults. If there is something stored in user defaults for "pullData", it will be used.
If you didn't want to use a fallback value then you'd have to have the property as a Bool? instead of a Bool:
#AppStorage("pullData") var pullData: Bool?
But optional booleans are a terrible idea, so don't do that.
Related
I have 2 radio buttons in the UI, one for setting a variable (ng-model) say selected as true (ng-value=true) and other one for setting it as false (ng-value=false). Now, when none of them is selected it results in the variable selected being absent from the outgoing request (as expected).
However, when that is dealt with Django Forms, the self.data dictionary in the clean() method gives False on accessing self.data.get('selected') / self.data['selected'] why is that so? Shouldn't it be None or at least give a key-error when it was not even present in the actual request?
Note that the variable 'selected' is actually a field in a Django Model with default=False, is that thing responsible for this behaviour? How can I circumvent this situation considering that altering the Django Model field isn't an option?
So I dealt with it the other day by checking for the selected key in the raw request.body. Now, since its a string, I had to parse it to a dict and then access the mentioned key using :
json.loads(request.body).get('selected')
In this way, if selected is not present at all when none of the radio buttons are selected, I get None. Similarly, if the radio button for ng-value=true is selected then I get True and vice-versa.
I have an attribute named session that is set to 1. In changeSession() I am trying to change its value to 2. In the last step session is printed and it is still 1. You can see the the code below. Doing the following would make us print the value 2:
session=changeSession()
let changeSession() return the new session value.
However, is it possible to get the value 2 printed without letting changeSession() return anything?
The code:
session=1
def set_session(s):
session=s
def changeSession():
set_session(2)
changeSession()
print session
To do that just use global session in set_session to indicate to python that you want to use the session that you defined outside of the function scope. If you want to document yourself about this behavior, it is called variable scope. Here is the fixed code :
session=1
def set_session(s):
global session
session=s
def changeSession():
set_session(2)
changeSession()
print session
Say you have a /new route, and the router creates a new instance of your model.
Your model's properties are bound to some input fields on the page.
I want to be able to detect if the new model has changed since it's instantiation.
model.get('hasDirtyAttributes') unfortunately reports true for new instances, because it has not been saved yet.
I thought to try model.get('hasDirtyAttributes') && model.get('dirtyType') === 'updated' but dirtyType is still created until you do the first save.
You can call model.changedAttributes() and see if it's empty (docs), but you can't observe it. Ex -
Object.keys(this.get('model').changedAttributes()).length > 0
I am not aware of a general attribute that you could observe in this case. You would have to observe a specified list of attributes.
or you could override set on your model and have it set a value -
set: (key, value) ->
#attributeHasChanged = true
#_super(key, value)
You could create a computed property on that model which would depend on any declared attribute and would return true if any of attributes differs from it's initial value, otherways false.
For better performance you could only compute this property if model.get('isNew') === true.
I thought this would be super simple but its turning into a pain in the butt.
I need to create a function that will accept a string and return it re-formatted as a URL slug. For example I'd pass in the string "The Adventures of Huckleberry Finn" and my function would return the string "the_adventures_of_huckleberry_finn". I have no problem doing the actual conversion. However in Ember actions can only return True, False or Undefined so the action always returns an object named Undefined rather than the string I told it to return.
How can I go about creating this function/action/computed property in my controller so that it correctly returns the formatted string? I have attempted to just add a normal JS function into the controller. I also tried to define this as a computed property but this action will be used only in the controller, the template has no need to know anything about the slug so a computed property dosn't really make sense and I couldn't get it to work anyways.
Ok, here is an example in how to achieve that: jsfiddle.net/NQKvy/837
I would return a computed property call something like slug
slug: function(){
return this.get('title').split(' ').join('_');
}.property('title')
Then you can use {{slug}} in your template of this.get('slug') in your controller.
Cheers
I have a list of items in a view called client_items. I want to be able to use the variable items_list`which is another view called edit_order in client_items. So is there a way to call the variable from a different view? (Import a variable from another view and be able to use this variable in the other) I cannot just write it in client_items view because it needs an order_no augment.
Edit: here is my latest views. I have tried creating another views called items_in_edit_order. At this point I get `order_no not defined.
def items_in_edit_order(order_no):
order = models.Order.objects.get(pk = order_no)
return order
def client_items(request, client_id = 0):
client = models.Client.objects.get(pk = client_id)
items = client.storageitem_set.all()
order = items_in_edit_order(order_no)
return render_to_response('items.html', {'items':items, 'client':client, 'order':order}, context_instance = RequestContext(request))
Just adding, since no one has said this and it seems like you don't understand this yet:
Your client_items view must, somehow, have access to the order_no variable. If for some reason the value is not being passed along via the URL, it must get the value from somehwere. There are only three real locations where it could get this value:
Database: this will work if you are, for example, storing something like a cart which is directly linked to a user. So for example, you might be able to do something like order_no = Order.objects.filter(cart__user=request.user).order_no which would get the order associated with the user's current cart, then return the order_no value.
Session: you store the order_no in the session. This would assume you had an earlier view where the value for order_no was set, at which point you would save it using request.session['order_no']=order_no. Later, when you wanted to retrieve the value, you would simply use order_no=request.session['order_no'] in the view.
Cookie: not really recommended, but an option nonetheless. It's complicated because in the first view you'd have to create the response object (as in resp = render_to_response(template_name, locals(), RequestContext(request)) and then write a cookie to it resp.set_cookie("order_no", order_no). You retrieve it using request.COOKIES['order_no']
There are other, bizarre, places you could store the value: files, cache, other data storage formats, etc. Not at all recommended.
No. Write a function that returns the value you're interested in, and call it from both views.
What the guy said above is correct. You shouldn't attempt to "share" variables to different views.
However in the event you must or have a reason to then you could just store it in the session and then you have access to it in any view that has access to the "request".
Hope that helps.